相关疑难解决方法(0)

如何将字符串拆分为列表?

我希望我的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 split list text-segmentation

545
推荐指数
8
解决办法
170万
查看次数

将字符串插入列表而不会拆分为字符

我是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)

搜索了文档和网络,但这不是我的一天.

python

104
推荐指数
5
解决办法
24万
查看次数

标签 统计

python ×2

list ×1

split ×1

text-segmentation ×1