Python split函数无法在列表上生成列表列表

2024-10-03 21:32:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在学习python,并做了以下实验

    text = "this is line one . this is line two . this is line three ."
    
    tokens = text.split(" ")            # split text into token with seperator "space"
    lioftokens = tokens.split(".")      # split tokens into list of tokens with seperator "dot"
    
    print(tokens)                       # output = ['this', 'is', 'line', 'one', '.', 'this', 'is', 'line', 'two', '.', 'this', 'is', 'line', 'three', '.']
    print(lioftokens)                   # expected output = [['this', 'is', 'line', 'one', '.'],
                                        #                    ['this', 'is', 'line', 'two', '.'],
                                        #                    ['this', 'is', 'line', 'three', '.']]

它给出的是错误,而不是预期的输出

split()代表字符串,而不是列表。 我该如何解决它

#IamNewToPython


Tags: textoutputiswithlinethisonesplit
3条回答

尝试使用list理解:

text = "this is line one . this is line two . this is line three ."
print([line.rstrip().split() for line in text.split('.') if line])

输出:

[['this', 'is', 'line', 'one'], ['this', 'is', 'line', 'two'], ['this', 'is', 'line', 'three']]

如果要保留拆分器,请尝试:

import re
text = "this is line one . this is line two . this is line three ."
print([line.rstrip().split() for line in re.split('([^\.]*\.)', text) if line])

输出:

[['this', 'is', 'line', 'one', '.'], ['this', 'is', 'line', 'two', '.'], ['this', 'is', 'line', 'three', '.']]

编辑:

如果要执行列表拆分,请尝试:

l = ['this', 'is', 'line', 'one', '.', 'this', 'is', 'line', 'two', '.', 'this', 'is', 'line', 'three', '.']
newl = [[]]
for i in l:
    newl[-1].append(i)
    if i == '.':
        newl.append([])
print(newl)

输出:

[['this', 'is', 'line', 'one', '.'], ['this', 'is', 'line', 'two', '.'], ['this', 'is', 'line', 'three', '.'], []]
text = "this is line one . this is line two . this is line three ."

# first split on the periods
sentences = text.split('.')

for s in sentences:
    # chop off trailing whitespace and then split on spaces
    print(s.rstrip().split())

这项工作:

>>> text = "this is line one . this is line two . this is line three ."
>>> list(filter(None, map(str.split, text.split("."))))
[['this', 'is', 'line', 'one'],
 ['this', 'is', 'line', 'two'],
 ['this', 'is', 'line', 'three']]

您可以简单地首先按.拆分列表,然后简单地将mapastr.split拆分为列表中的每个字符串

相关问题 更多 >