lines = [["absb asdjhasd", "sadjhj sdhjsdh"], ["asjhjhad asdasd", " ashjhfdj asdasd"]]
for z in lines:
for l in z:
l = l.split()
print lines
我如何实现这些行现在包含分裂?似乎某处有副本? Python 2.7
答案 0 :(得分:2)
l.split()
返回拆分结果而不影响原始字符串对象,l =
仅重新绑定变量,而不是列表条目。
您也必须跟踪索引:
for z in lines:
for index, l in enumerate(z):
z[index] = l.split()
或者你可以使用nested list comprehension来重建字符串split:
lines = [[l.split() for l in z] for z in lines]
答案 1 :(得分:0)
您可以使用它来将字符串替换为其分割
lines = [[l.split() for l in line] for line in lines]