如何将一个句子分割并将每个单词存储在列表中?例如:
"these are words" ⟶ ["these", "are", "words"]
如果想要根据其他分隔符进行分割,请参见 使用Python按分隔符拆分字符串。
如果想要将字符串拆分为单个字符,请参见 如何将字符串拆分为字符列表?。
如何将一个句子分割并将每个单词存储在列表中?例如:
"these are words" ⟶ ["these", "are", "words"]
如果想要根据其他分隔符进行分割,请参见 使用Python按分隔符拆分字符串。
如果想要将字符串拆分为单个字符,请参见 如何将字符串拆分为字符列表?。
sentence
,将其中每个单词存入名为 words
的列表中。words = sentence.split()
将字符串 text
按任何连续的空格分割:
words = text.split()
要按自定义分隔符,如","
来分割字符串text
:
words = text.split(",")
words
变量将是一个 list
,其中包含从 text
按分隔符分割得到的单词。
使用 str.split()
:
返回字符串中的单词列表,使用sep作为分隔符... 如果未指定sep或为
None
,则将应用不同的拆分算法:连续的空格被视为单个分隔符,并且如果字符串具有前导或尾随空格,则结果将不包含开头或结尾的空字符串。
>>> line = "a sentence with a few words"
>>> line.split()
['a', 'sentence', 'with', 'a', 'few', 'words']
根据你计划对列表式语句做什么,你可能想要查看自然语言工具包。它专注于文本处理和评估,并可以用来解决你的问题。
import nltk
words = nltk.word_tokenize(raw_sentence)
这样做的另一个好处是可以将标点符号拆开。>>> 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()
更好。 - hobswe'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']
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
print(list("word"))
# ['w', 'o', 'r', 'd']
print(list("some sentence"))
# ['s', 'o', 'm', 'e', ' ', 's', 'e', 'n', 't', 'e', 'n', 'c', 'e']
str.split()
将以下字符串分割为:s = "Hi, these are words; these're, also, words."
words = s.split()
# ['Hi,', 'these', 'are', 'words;', "these're,", 'also,', 'words.']
在编程中,Hi
、words
、also
等单词后面都有标点符号。Python内置了一个string
模块,其中包含一个标点符号的字符串属性(string.punctuation
)。消除标点符号的一种方法是从每个单词中简单地将其删除:
import string
words = [w.strip(string.punctuation) for w in s.split()]
# ['Hi', 'these', 'are', 'words', "these're", 'also', 'words']
table = str.maketrans('', '', string.punctuation)
words = s.translate(table).split()
# ['Hi', 'these', 'are', 'words', 'thesere', 'also', 'words']
它无法处理像these're
这样的单词,因此可以使用nltk.word_tokenize
来处理tgray建议。只需过滤掉完全由标点符号组成的单词即可。
import nltk
words = [w for w in nltk.word_tokenize(s) if w not in string.punctuation]
# ['Hi', 'these', 'are', 'words', 'these', "'re", 'also', 'words']
将单词拆分,不损害单词内的撇号。请找出输入_1和输入_2中的Moore's law。
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']
print(word)
。 - tgray