我正在使用Python编程语言,我的列表如下:
我希望您能将字符串中每个句子中的单词分开并存储到新列表 new_list 中。new_list 将会是这样的:
str = ["Hello dude", "What is your name", "My name is Chetan"]
我希望您能将字符串中每个句子中的单词分开并存储到新列表 new_list 中。new_list 将会是这样的:
new_list = ["Hello", "dude", "What", "is", "your", "name", "My", "name",
"is", "Chetan"]
我尝试使用这段代码
for row in str:
new_list.append(row.split(" "))
输出:
[['Hello', 'dude'], ['What', 'is', 'your', 'name'], ['My', 'name', 'is',
'Chetan']]
这是一个列表的列表
str
作为变量名,因为这会掩盖内置的str
。 - Ryan HainingconcatMap words originalList
- Adam Smithsplit()
而不是split(' ')
可以处理多个连续空格字符的情况。 - Ryan Haining