3

I have a list in Python that looks like this:

["Hello, My Name is John", "Good Afternoon, my name is David", "I am three years old"]

I would like to split each string into a comma-separated list and store the result, and also convert each word to lowercase:

[['hello','my','name','is','john'], ['good','afternoon','my','name','is','david'],['i','am','three','years','old']]

Any suggestions how this could be done? Thank you.

Thomas Moore
  • 941
  • 2
  • 11
  • 17

6 Answers6

3

You can simply replace the comma with space and strip the rest of the string.

strList = ["Hello, My Name is John", "Good Afternoon, my name is David", "I am three years old"]
[i.lower().replace(',', '').split() for i in strList]
sanyam
  • 96
  • 5
1

You can split each string and then filter out the commas to get the list of lists you want.

a = ["Hello, My Name is John", "Good Afternoon, my name is David", "I am three years old"]
b = [[j.lower().replace(',', '') for j in i.split()] for i in a]

b
'''
Outputs:[['hello', 'my', 'name', 'is', 'john'],
         ['good', 'afternoon', 'my', 'name', 'is', 'david'],
         ['i', 'am', 'three', 'years', 'old']]
'''
Osman Mamun
  • 2,864
  • 1
  • 16
  • 22
  • 1
    Almost there ... `hello` keeps its comma here, I think you want something other than `j!= ','` since `j` here is each word :) – LeKhan9 Mar 04 '19 at 05:21
1

Try below code:

x = ["Hello, My Name is John", "Good Afternoon, my name is David", "I am three years old"]

z = []

for i in x:
    # Replacing "," , converting to lower and then splitting
    z.append(i.replace(","," ").lower().split())

print z

Output:

[['hello', 'my', 'name', 'is', 'john'], ['good', 'afternoon', 'my', 'name', 'is', 'david'], ['i', 'am', 'three', 'years', 'old']]
skaul05
  • 2,154
  • 3
  • 16
  • 26
1
import re

def split_and_lower(s): 
    return list(map(str.lower, re.split(s, '[^\w]*'))) 

L = ["Hello, My Name is John", "Good Afternoon, my name is David", "I am three years old"] 
result = list(map(split_and_lower, L))
print(result)

Output:

[['hello', 'my', 'name', 'is', 'john'],
 ['good', 'afternoon', 'my', 'name', 'is', 'david'],
 ['i', 'am', 'three', 'years', 'old']]
BallpointBen
  • 9,406
  • 1
  • 32
  • 62
1

I'll go with replace and split.

strlist = ["Hello, My Name is John", "Good Afternoon, my name is David", "I am three years old"]
>>>[x.replace(',','').lower().split() for x in strlist]
[['hello', 'my', 'name', 'is', 'john'], ['good', 'afternoon', 'my', 'name', 'is', 'david'], ['i', 'am', 'three', 'years', 'old']]
kerwei
  • 1,822
  • 1
  • 13
  • 22
1

An approach using rstrip on each word :)

ls = ["Hello, My Name is John", "Good Afternoon, my name is David", "I am three years old"]

output_ls = [[word.lower().rstrip(',') for word in sentence.split()] for sentence in ls]

output:

[['hello', 'my', 'name', 'is', 'john'], ['good', 'afternoon', 'my', 'name', 'is', 'david'], ['i', 'am', 'three', 'years', 'old']]
LeKhan9
  • 1,300
  • 1
  • 5
  • 15