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

Tha*_*anx 545 python split list text-segmentation

我希望我的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)

nst*_*ehr 469

text.split()
Run Code Online (Sandbox Code Playgroud)

这应足以将每个单词存储在列表中. words已经是句子中单词的列表,因此不需要循环.

其次,它可能是一个错字,但你的循环有点搞砸了.如果你确实想要使用append,它将是:

words.append(word)
Run Code Online (Sandbox Code Playgroud)

word.append(words)
Run Code Online (Sandbox Code Playgroud)

  • 这并没有消除特殊符号,例如逗号和点 (3认同)

zal*_*lew 432

text在任何连续的空白运行中拆分字符串.

words = text.split()      
Run Code Online (Sandbox Code Playgroud)

text在分隔符中拆分字符串:",".

words = text.split(",")   
Run Code Online (Sandbox Code Playgroud)

单词变量将是a list并包含text分隔符上split 的单词.


gim*_*mel 84

str.split()

返回字符串中的单词列表,使用sep作为分隔符...如果未指定sep或为None,则应用不同的分割算法:连续空格的运行被视为单个分隔符,结果将包含如果字符串具有前导或尾随空格,则在开头或结尾没有空字符串.

>>> line="a sentence with a few words"
>>> line.split()
['a', 'sentence', 'with', 'a', 'few', 'words']
>>> 
Run Code Online (Sandbox Code Playgroud)


tgr*_*ray 51

根据您打算如何处理您的句子列表,您可能需要查看Natural Language Took Kit.它主要涉及文本处理和评估.您也可以使用它来解决您的问题:

import nltk
words = nltk.word_tokenize(raw_sentence)
Run Code Online (Sandbox Code Playgroud)

这具有分割标点符号的额外好处.

例:

>>> import nltk
>>> s = "The fox's foot grazed the sleeping dog, waking it."
>>> words = nltk.word_tokenize(s)
>>> words
['The', 'fox', "'s", 'foot', 'grazed', 'the', 'sleeping', 'dog', ',', 
'waking', 'it', '.']
Run Code Online (Sandbox Code Playgroud)

这允许您过滤掉您不想要的任何标点符号并仅使用单词.

请注意,string.split()如果您不打算对句子进行任何复杂的操作,则使用的其他解决方案会更好.

将帖子

  • `split()`依赖于white-space作为分隔符,因此它将无法分隔带连字符的单词 - 而长短划分的短语也将无法分割.如果句子中包含任何没有空格的标点符号,那么这些标点符号就会无效.对于任何真实的文本解析(比如这个评论),你的nltk建议比split()更好. (4认同)
  • 可能有用,虽然我不认为这是分裂成"单词".通过任何简单的英语定义,'','和'''s``不是单词.通常情况下,如果你想以符号标注的方式将上面的句子分成"单词",你就要删除逗号并将"狐狸"作为单个单词. (2认同)

Col*_*nic 29

这个算法怎么样?在空格上拆分文本,然后修剪标点符号.这样可以小心地删除单词边缘的标点符号,而不会损坏单词中的撇号we're.

>>> text
"'Oh, you can't help that,' said the Cat: 'we're all mad here. I'm mad. You're mad.'"

>>> text.split()
["'Oh,", 'you', "can't", 'help', "that,'", 'said', 'the', 'Cat:', "'we're", 'all', 'mad', 'here.', "I'm", 'mad.', "You're", "mad.'"]

>>> import string
>>> [word.strip(string.punctuation) for word in text.split()]
['Oh', 'you', "can't", 'help', 'that', 'said', 'the', 'Cat', "we're", 'all', 'mad', 'here', "I'm", 'mad', "You're", 'mad']
Run Code Online (Sandbox Code Playgroud)

  • 很好,但有些英文单词确实包含尾随标点符号.例如,"eg"和"Mrs."中的尾随点以及占有式"frogs"中的尾随撇号(如"青蛙的腿"中)是该单词的一部分,但将被此算法剥离.正确处理缩写可以*大致*通过检测点分离的首字母加上使用特殊情况的字典(如`Mr.`,`Mrs.)来实现.将占有撇号与单引号区分开来要困难得多,因为它需要解析包含单词的句子的语法. (4认同)
  • @MarkAmery你是对的.此后我发现一些标点符号 - 例如em破折号 - 可以分隔没有空格的单词. (2认同)

dbr*_*dbr 15

我希望我的python函数分割句子(输入)并将每个单词存储在列表中

str().split()方法执行此操作,它接受一个字符串,将其拆分为一个列表:

>>> the_string = "this is a sentence"
>>> words = the_string.split(" ")
>>> print(words)
['this', 'is', 'a', 'sentence']
>>> type(words)
<type 'list'> # or <class 'list'> in Python 3.0
Run Code Online (Sandbox Code Playgroud)

你遇到的问题是因为一个错字,你写的print(words)而不是print(word):

word变量重命名为current_word,这就是你所拥有的:

def split_line(text):
    words = text.split()
    for current_word in words:
        print(words)
Run Code Online (Sandbox Code Playgroud)

..当你应该做的时候:

def split_line(text):
    words = text.split()
    for current_word in words:
        print(current_word)
Run Code Online (Sandbox Code Playgroud)

如果由于某种原因你想在for循环中手动构造一个列表,你可以使用list append()方法,也许是因为你想要小写所有单词(例如):

my_list = [] # make empty list
for current_word in words:
    my_list.append(current_word.lower())
Run Code Online (Sandbox Code Playgroud)

或者更整洁,使用列表理解:

my_list = [current_word.lower() for current_word in words]
Run Code Online (Sandbox Code Playgroud)


Tar*_*win 11

shlex有一个.split()功能.它的不同之处str.split()在于它不保留引号并将引用的短语视为单个单词:

>>> import shlex
>>> shlex.split("sudo echo 'foo && bar'")
['sudo', 'echo', 'foo && bar']
Run Code Online (Sandbox Code Playgroud)

  • 谨慎使用,尤其是 NLP。它会在单引号字符串上崩溃,例如“It's good.”和“ValueError:没有结束引号” (2认同)

Bla*_*ard 10

如果要在列表中包含单词/句子的所有字符,请执行以下操作:

print(list("word"))
#  ['w', 'o', 'r', 'd']


print(list("some sentence"))
#  ['s', 'o', 'm', 'e', ' ', 's', 'e', 'n', 't', 'e', 'n', 'c', 'e']
Run Code Online (Sandbox Code Playgroud)