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)
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
返回字符串中的单词列表,使用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()
如果您不打算对句子进行任何复杂的操作,则使用的其他解决方案会更好.
将帖子
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)
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)
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)
归档时间: |
|
查看次数: |
1697232 次 |
最近记录: |