Python字符串拆分数组

Python字符串拆分数组,python,Python,我有一个Python列表,如下所示: ["Hello, My Name is John", "Good Afternoon, my name is David", "I am three years old"] 我希望将每个字符串拆分为逗号分隔的列表并存储结果,同时将每个单词转换为小写: [['hello','my','name','is','john'], ['good','afternoon','my','name','is','david'],['i','am','three','yea

我有一个Python列表,如下所示:

["Hello, My Name is John", "Good Afternoon, my name is David", "I am three years old"]
我希望将每个字符串拆分为逗号分隔的列表并存储结果,同时将每个单词转换为小写:

[['hello','my','name','is','john'], ['good','afternoon','my','name','is','david'],['i','am','three','years','old']]
有什么建议可以这样做吗?
谢谢。

您可以拆分每个字符串,然后过滤掉逗号以获得所需列表的列表

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']]
'''
请尝试以下代码:

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
输出:

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

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

我会选择替换和拆分

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']]

在每个单词上使用rstrip的方法:)

输出:

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

您可以简单地用空格替换逗号,并去掉字符串的其余部分

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]

[[j.lower()代表i.replace(“,”,“”)中的j.split()]代表mylist中的i]
@itzMEonTV您在剥离commasAlmost时错过了<代码>你好在这里保留逗号,我想你想要的不是
j!=','因为
j
这里是每个单词:)