我希望我的python函数分割一个句子(输入)并将每个单词存储在一个列表中。我当前的代码将句子拆分,但不将单词存储为列表。我该怎么做?


10def split_line(text):
# split the text
words = text.split()
# for each word in the line:
for word in words:
# print the word
print(words)

此代码的哪些部分不起作用?您能提供错误信息或您遇到的问题吗?

实际上,您将打印列表中每个单词的完整单词列表。我认为你打算用print(word)作为最后一行。

1text.split()

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

第二,这可能是一个打字错误,但你的循环有点混乱。如果您真的想使用append,它将是:

1words.append(word)

1word.append(words)

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

1words = text.split()

在分隔符:","上拆分text中的字符串。

1words = text.split(",")

words变量将是一个list并包含分隔符上text拆分的单词。

分裂()

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.


4>>> line="a sentence with a few words"
>>> line.split()
['a', 'sentence', 'with', 'a', 'few', 'words']
>>>
@warvariuc-应该链接到docs.python.org/2/library/stdtypes.html str.split

根据你计划如何处理你的句子列表,你可能想看看自然语言的工具包。它主要处理文本处理和评估。您还可以使用它来解决您的问题:

2import nltk
words = nltk.word_tokenize(raw_sentence)

这样做还有一个额外的好处,那就是拆分标点符号。

例子:


6>>> 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', '.']

这允许您过滤掉不需要的标点符号,只使用单词。

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

[编辑]

split()依赖于空格作为分隔符,因此它将无法分隔连字符单词,长划分隔短语也将无法拆分。如果句子中包含任何没有空格的标点符号,这些标点符号将无法粘贴。对于任何真实的文本解析(比如这个注释),您的nltk建议比split()好得多。

可能有用,尽管我不会将其描述为拆分为"单词"。根据任何简单的英语定义,','和"'s"不是单词。通常,如果你想用标点符号的方式把上面的句子分割成"单词",你可以去掉逗号,把"fox's"作为一个单词。

截至2016年4月,python 2.7+。

这个算法怎么样?在空白处拆分文本,然后修剪标点符号。这会小心地删除单词边缘的标点符号,而不会损害单词内部的撇号,如we're。


9>>> 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']

很好,但有些英语单词确实包含尾随标点。例如,e.g.和Mrs.中的尾随点和所有格frogs'中的尾随撇号(如frogs' legs中的尾随撇号)是单词的一部分,但将由该算法去除。正确处理缩写词可以通过检测点分隔的初始化和使用特殊情况字典(如Mr.和Mrs.来大致实现。区分所有格撇号和单引号是非常困难的,因为它需要解析包含单词的句子的语法。

@Markamery你说得对。在我看来,有些标点符号,比如em破折号,可以不加空格地分隔单词。

I want my python function to split a sentence (input) and store each word in a list

str().split()方法是这样做的,它使用一个字符串,将其拆分为一个列表:


6>>> the_string ="this is a sentence"
>>> words = the_string.split("")
>>> print(words)
['this', 'is', 'a', 'sentence']
>>> type(words)
 # or in Python 3.0

你的问题是因为打字错误,你写的是print(words),而不是print(word):

将word变量重命名为current_word,这是您拥有的:


4def split_line(text):
words = text.split()
for current_word in words:
print(words)

…当你应该这样做的时候:

4def split_line(text):
words = text.split()
for current_word in words:
print(current_word)

如果出于某种原因,您希望在for循环中手动构造一个列表,那么您将使用list append()方法,这可能是因为您希望降低所有单词的大小写(例如):

3my_list = [] # make empty list
for current_word in words:
my_list.append(current_word.lower())

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

1my_list = [current_word.lower() for current_word in words]

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

3>>> import shlex
>>> shlex.split("sudo echo 'foo && bar'")
['sudo', 'echo', 'foo && bar']

我想你是因为打字错误而困惑。

用循环中的print(word)替换print(words),使每个字都打印在不同的行上。

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


6print(list("word"))
# ['w', 'o', 'r', 'd']
print(list("some sentence"))
# ['s', 'o', 'm', 'e', ' ', 's', 'e', 'n', 't', 'e', 'n', 'c', 'e']