我希望我的Python函数分割一个句子(输入)并将每个单词存储在一个列表中.我当前的代码拆分了句子,但没有将单词存储为列表.我怎么做?
def split_line(text):
# split the text
words = text.split()
# for each word in the line:
for word in words:
# print the word
print(words)
Run Code Online (Sandbox Code Playgroud) 我是Python的新手,无法找到将字符串插入列表的方法,而不会将其拆分为单个字符:
>>> list=['hello','world']
>>> list
['hello', 'world']
>>> list[:0]='foo'
>>> list
['f', 'o', 'o', 'hello', 'world']
Run Code Online (Sandbox Code Playgroud)
我该怎么做:
['foo', 'hello', 'world']
Run Code Online (Sandbox Code Playgroud)
搜索了文档和网络,但这不是我的一天.