我正在学习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
尝试使用
list
理解:输出:
如果要保留拆分器,请尝试:
输出:
编辑:
如果要执行列表拆分,请尝试:
输出:
这项工作:
您可以简单地首先按
.
拆分列表,然后简单地将map
astr.split
拆分为列表中的每个字符串相关问题 更多 >
编程相关推荐