如何将文本列表拆分为单词Python列表

How to split a list of text into a list of words Python

本问题已经有最佳答案,请猛点这里访问。

我有这个清单:

1
["This is a set of words and it is not correct"]

我想这样做:

1
["This","Is","A"] ...

我该怎么做


1
"This is a set of words and it is not correct".title().split()

输出:

1
['This', 'Is', 'A', 'Set', 'Of', 'Words', 'And', 'It', 'Is', 'Not', 'Correct']


你可以这样做。

1
2
a ="This is a set of words and it is not correct"
[i.capitalize() for i in a.split()]

如果输入是您在问题中提到的list

1
2
a = ["This is a set of words and it is not correct"]
[i.capitalize() for i in a[0].split()]

产量

['This', 'Is', 'A', 'Set', 'Of', 'Words', 'And', 'It', 'Is', 'Not',
'Correct']


使用split()方法

1
2
3
>>> foo = ["This is a set of words and it is not correct"]
>>> foo[0].split()
['This', 'is', 'a', 'set', 'of', 'words', 'and', 'it', 'is', 'not', 'correct']