如何拆分句子并将每个单词存储在列表中?
"these are words" ⟶ ["these", "are", "words"]
print(word)
作为最后一行。
给定一个字符串 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']
根据您计划对句子列表执行的操作,您可能需要查看 Natural Language Took Kit。它主要处理文本处理和评估。您也可以使用它来解决您的问题:
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() 好得多。
','
和 "'s"
都不是单词。通常,如果您想以标点符号感知的方式将上面的句子拆分为“单词”,您需要去掉逗号并将 "fox's"
作为单个单词。
这个算法怎么样?在空白处拆分文本,然后修剪标点符号。这会小心地从单词边缘删除标点符号,而不会损害诸如 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']
e.g.
和 Mrs.
中的尾随点以及所有格 frogs'
中的尾随撇号(如 frogs' legs
)是单词的一部分,但会被此算法剥离。正确处理缩写可以大致通过检测点分隔的首字母并使用特殊情况字典(如 Mr.
、Mrs.
)来实现。区分所有格撇号和单引号要困难得多,因为它需要解析包含该单词的句子的语法。
我希望我的 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
您遇到的问题是因为拼写错误,您写的是 print(words)
而不是 print(word)
:
将 word
变量重命名为 current_word
,这就是您所拥有的:
def split_line(text):
words = text.split()
for current_word in words:
print(words)
..当你应该做的时候:
def split_line(text):
words = text.split()
for current_word in words:
print(current_word)
如果出于某种原因您想在 for 循环中手动构建一个列表,您将使用 list append()
方法,可能是因为您想将所有单词小写(例如):
my_list = [] # make empty list
for current_word in words:
my_list.append(current_word.lower())
或者更简洁一点,使用 list-comprehension:
my_list = [current_word.lower() for current_word in words]
如果您想要列表中的单词/句子的所有字符,请执行以下操作:
print(list("word"))
# ['w', 'o', 'r', 'd']
print(list("some sentence"))
# ['s', 'o', 'm', 'e', ' ', 's', 'e', 'n', 't', 'e', 'n', 'c', 'e']
我认为你因为错字而感到困惑。
在循环中将 print(words)
替换为 print(word)
以使每个单词都打印在不同的行上
在不损害单词内部撇号的情况下拆分单词 请找到 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']
不定期副业成功案例分享