Return a list of the words in the string, using sep as the delimiter
... If sep is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace.
>>> line="a sentence with a few words"
>>> line.split()
['a', 'sentence', 'with', 'a', 'few', 'words']
>>>
str.split()
这应该足够存储列表中的每个单词。
words
已经是句子中单词的列表,因此不需要循环。第二,可能是输入错误,但你的循环有点混乱。如果您真的想使用append,它将是:
不是
在
text
中对任何连续的空白行拆分字符串。在分隔符
text
中拆分字符串:","
。words变量将是一个
list
,并且包含分隔符上text
中拆分的单词。相关问题 更多 >
编程相关推荐