Python 如何将字符串拆分为列表?

Python 如何将字符串拆分为列表?,python,list,split,text-segmentation,Python,List,Split,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) print(lis

我希望我的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)
print(list("word"))
#  ['w', 'o', 'r', 'd']


print(list("some sentence"))
#  ['s', 'o', 'm', 'e', ' ', 's', 'e', 'n', 't', 'e', 'n', 'c', 'e']

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

words = text.split()      
print(list("word"))
#  ['w', 'o', 'r', 'd']


print(list("some sentence"))
#  ['s', 'o', 'm', 'e', ' ', 's', 'e', 'n', 't', 'e', 'n', 'c', 'e']
在分隔符:
“,”
上的
文本中拆分字符串

print(list("word"))
#  ['w', 'o', 'r', 'd']


print(list("some sentence"))
#  ['s', 'o', 'm', 'e', ' ', 's', 'e', 'n', 't', 'e', 'n', 'c', 'e']
words变量将是一个
列表
,包含分隔符上拆分的
文本中的单词

text.split()
print(list("word"))
#  ['w', 'o', 'r', 'd']


print(list("some sentence"))
#  ['s', 'o', 'm', 'e', ' ', 's', 'e', 'n', 't', 'e', 'n', 'c', 'e']
这应该足以将每个单词存储在一个列表中
words
已经是句子中单词的列表,因此不需要循环

print(list("word"))
#  ['w', 'o', 'r', 'd']


print(list("some sentence"))
#  ['s', 'o', 'm', 'e', ' ', 's', 'e', 'n', 't', 'e', 'n', 'c', 'e']
第二,它可能是一个输入错误,但你的循环有点混乱。如果您确实想使用append,那么应该是:

words.append(word)
print(list("word"))
#  ['w', 'o', 'r', 'd']


print(list("some sentence"))
#  ['s', 'o', 'm', 'e', ' ', 's', 'e', 'n', 't', 'e', 'n', 'c', 'e']
不是

print(list("word"))
#  ['w', 'o', 'r', 'd']


print(list("some sentence"))
#  ['s', 'o', 'm', 'e', ' ', 's', 'e', 'n', 't', 'e', 'n', 'c', 'e']

print(list("word"))
#  ['w', 'o', 'r', 'd']


print(list("some sentence"))
#  ['s', 'o', 'm', 'e', ' ', 's', 'e', 'n', 't', 'e', 'n', 'c', 'e']
使用sep作为分隔符,返回字符串中单词的列表 ... 如果未指定sep或sep为None,则应用不同的拆分算法:连续空格的运行被视为单个分隔符,如果字符串具有前导或尾随空格,则结果的开头或结尾将不包含空字符串

print(list("word"))
#  ['w', 'o', 'r', 'd']


print(list("some sentence"))
#  ['s', 'o', 'm', 'e', ' ', 's', 'e', 'n', 't', 'e', 'n', 'c', 'e']

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

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)
,使每个单词都打印在不同的行上

print(list("word"))
#  ['w', 'o', 'r', 'd']


print(list("some sentence"))
#  ['s', 'o', 'm', 'e', ' ', 's', 'e', 'n', 't', 'e', 'n', 'c', 'e']
我希望我的python函数拆分一个句子(输入),并将每个单词存储在一个列表中

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()

>>> 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']
…当你应该做的时候:

def split_line(text):
    words = text.split()
    for current_word in words:
        print(current_word)
print(list("word"))
#  ['w', 'o', 'r', 'd']


print(list("some sentence"))
#  ['s', 'o', 'm', 'e', ' ', 's', 'e', 'n', 't', 'e', 'n', 'c', 'e']
如果出于某种原因,您想在for循环中手动构造一个列表,那么您可以使用list
append()
方法,可能是因为您想将所有单词的大小写降低(例如):

print(list("word"))
#  ['w', 'o', 'r', 'd']


print(list("some sentence"))
#  ['s', 'o', 'm', 'e', ' ', 's', 'e', 'n', 't', 'e', 'n', 'c', 'e']
或更整洁一点,使用:

print(list("word"))
#  ['w', 'o', 'r', 'd']


print(list("some sentence"))
#  ['s', 'o', 'm', 'e', ' ', 's', 'e', 'n', 't', 'e', 'n', 'c', 'e']

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

import nltk
words = nltk.word_tokenize(raw_sentence)
print(list("word"))
#  ['w', 'o', 'r', 'd']


print(list("some sentence"))
#  ['s', 'o', 'm', 'e', ' ', 's', 'e', 'n', 't', 'e', 'n', 'c', 'e']
这还有一个额外的好处,那就是将标点符号分开

print(list("word"))
#  ['w', 'o', 'r', 'd']


print(list("some sentence"))
#  ['s', 'o', 'm', 'e', ' ', 's', 'e', 'n', 't', 'e', 'n', 'c', 'e']
例如:

>>> 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', '.']
print(list("word"))
#  ['w', 'o', 'r', 'd']


print(list("some sentence"))
#  ['s', 'o', 'm', 'e', ' ', 's', 'e', 'n', 't', 'e', 'n', 'c', 'e']
这允许你过滤掉任何你不想要的标点符号,只使用单词

print(list("word"))
#  ['w', 'o', 'r', 'd']


print(list("some sentence"))
#  ['s', 'o', 'm', 'e', ' ', 's', 'e', 'n', 't', 'e', 'n', 'c', 'e']
请注意,如果您不打算对句子进行任何复杂的操作,那么使用
string.split()
的其他解决方案会更好

print(list("word"))
#  ['w', 'o', 'r', 'd']


print(list("some sentence"))
#  ['s', 'o', 'm', 'e', ' ', 's', 'e', 'n', 't', 'e', 'n', 'c', 'e']

[编辑]

这个算法怎么样?在空白处拆分文本,然后修剪标点符号。这会小心地删除单词边缘的标点符号,而不会损害单词内部的撇号,例如
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']
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()
的不同之处在于它不保留引号,并将引用的短语视为单个单词:

>>> import shlex
>>> shlex.split("sudo echo 'foo && bar'")
['sudo', 'echo', 'foo && bar']
print(list("word"))
#  ['w', 'o', 'r', 'd']


print(list("some sentence"))
#  ['s', 'o', 'm', 'e', ' ', 's', 'e', 'n', 't', 'e', 'n', 'c', 'e']

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

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

print(list("word"))
#  ['w', 'o', 'r', 'd']


print(list("some sentence"))
#  ['s', 'o', 'm', 'e', ' ', 's', 'e', 'n', 't', 'e', 'n', 'c', 'e']

拆分单词,但不要损坏单词内部的撇号 请找到输入摩尔定律

print(list("word"))
#  ['w', 'o', 'r', 'd']


print(list("some sentence"))
#  ['s', 'o', 'm', 'e', ' ', 's', 'e', 'n', 't', 'e', 'n', 'c', 'e']
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)
作为最后一行。
split()
依赖于空格作为分隔符,因此它将无法分隔连字符的单词,而长破折号分隔的短语也将无法拆分。如果句子中包含任何没有空格的标点符号,那么这些标点符号将无法坚持。对于任何现实世界的文本解析(如此评论),您的nltk建议都比split()好得多。可能有用,但我不会将其描述为拆分为“单词”。根据任何简单的英语定义,
,“
都不是单词。通常,如果您想以标点符号识别的方式将上述句子拆分为“单词”,您需要去掉逗号并将
“fox's”
作为单个单词。自2016年4月起,Python 2.7+。@warvariuc-应该链接到Nice,但有些英语单词确实包含尾随标点符号。例如,
中的尾随点,例如
夫人
,以及所有格
青蛙的
(如
青蛙的腿
)中的尾随撇号,都是单词的一部分,但会被此算法去除。正确处理缩写可以通过检测点分隔的首字母加上使用特殊情况字典(如
Mr.
Mrs.
)大致实现。区分所有格撇号和单引号要困难得多,因为它需要分析包含单词的句子的语法。@MarkAmery你说得对。我也曾想到,一些标点符号,如em破折号,可以在没有空格的情况下分隔单词。请谨慎使用,尤其是NLP。它将在单引号字符串上崩溃,如
“It's good.”
with
ValueError:No closing quote