我如何分割一个句子并将每个单词存储在一个列表中?例如,给定一个像“these are words”这样的字符串,我如何得到一个像["these", "are", "words"]这样的列表?


若要拆分其他分隔符,请参见在python中使用分隔符拆分字符串。

若要拆分为单个字符,请参见如何将字符串拆分为字符列表?。


当前回答

Shlex有一个.split()函数。它与str.split()的不同之处在于它不保留引号,将引用的短语视为单个单词:

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

注意:它适用于类unix的命令行字符串。它不适用于自然语言处理。

其他回答

这个算法呢?在空格上拆分文本,然后修剪标点符号。这样可以小心地去掉单词边缘的标点符号,而不会损害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']

在任意连续运行的空格上拆分字符串文本:

words = text.split()      

使用自定义分隔符(如",")分隔字符串文本:

words = text.split(",")   

words变量将是一个列表,并包含分隔符上分割的文本中的单词。

给定一个字符串句子,它将每个单词存储在一个名为words的列表中:

words = sentence.split()

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

在不伤害撇号的情况下拆分单词 请找到input_1和input_2摩尔定律

def split_into_words(line):
    import re
    word_regex_improved = r"(\w[\w']*\w|\w)"
    word_matcher = re.compile(word_regex_improved)
    return word_matcher.findall(line)

#Example 1

input_1 = "computational power (see Moore's law) and "
split_into_words(input_1)

# output 
['computational', 'power', 'see', "Moore's", 'law', 'and']

#Example 2

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

split_into_words(input_2)
#output
['Oh',
 'you',
 "can't",
 'help',
 'that',
 'said',
 'the',
 'Cat',
 "we're",
 'all',
 'mad',
 'here',
 "I'm",
 'mad',
 "You're",
 'mad']