【问题标题】:Python split function won't work on list to generate list of listPython split 函数不适用于列表以生成列表列表
【发布时间】:2021-04-26 15:33:18
【问题描述】:

我正在学习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

【问题讨论】:

  • tokens.split(".") 不起作用,因为 split 是将字符串拆分为列表,但 tokens 是列表而不是字符串

标签: python python-3.x string list split


【解决方案1】:

尝试使用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', '.'], []]

【讨论】:

  • 主要关心的是转换['this', 'is', 'line', 'one', '.', 'this', 'is', 'line', 'two' , '.', 'this', 'is', 'line', 'three', '.'] 进入列表列表 [['this', 'is', 'line', 'one', '.' ], ['this', 'is', 'line', '二', '.'], ['this', 'is', 'line', '三', '.']]
  • @ShaidaMuhammad 完成。请检查我的 Edit: 部分,如果可行,请记得接受并投票
【解决方案2】:

str.split() 方法。

text = "this is line one . this is line two . this is line three ."

print([text.split()[i:i+5] for i in range(0,len(text.split()),5) ])

【讨论】:

  • 主要关心的是转换['this', 'is', 'line', 'one', '.', 'this', 'is', 'line', 'two' , '.', 'this', 'is', 'line', 'three', '.'] 进入列表列表 [['this', 'is', 'line', 'one', '.' ], ['this', 'is', 'line', '二', '.'], ['this', 'is', 'line', '三', '.']]
【解决方案3】:
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())

【讨论】:

  • OP 希望将句点保留在输出中。
  • 主要关心的是转换['this', 'is', 'line', 'one', '.', 'this', 'is', 'line', 'two' , '.', 'this', 'is', 'line', 'three', '.'] 进入列表列表 [['this', 'is', 'line', 'one', '.' ], ['this', 'is', 'line', 'two', '.'], ['this', 'is', 'line', '三', '.']]
【解决方案4】:

这行得通:

>>> 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']]

您可以先通过. 简单地将列表拆分,然后将mapstr.split 简单地拆分为列表中的每个单独的字符串。

【讨论】:

  • OP 希望将句点保留在输出中。
  • 主要关心的是转换['this', 'is', 'line', 'one', '.', 'this', 'is', 'line', 'two' , '.', 'this', 'is', 'line', 'three', '.'] 进入列表列表 [['this', 'is', 'line', 'one', '.' ], ['this', 'is', 'line', '二', '.'], ['this', 'is', 'line', '三', '.']]
猜你喜欢
  • 2017-07-25
  • 1970-01-01
  • 2017-04-05
  • 1970-01-01
  • 2011-05-24
  • 1970-01-01
  • 2014-04-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多