【发布时间】: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