我在python中有一个列表
l= [[1105.46, 1105.75, 1105.75, 1105.46, 1051.46],
[ 120.23, 120.23, 120.41, 20.41, 120.23]]
我怎么能得到这个:
answer = [[1105.46,120.23], ....[1051.46,120.23]]
我这样做是:
answer = [[x, y] for x, y in l]
print answer
ValueError:太多值无法解包
使用zip()
标准python中的函数:
l= [[1105.46, 1105.75, 1105.75, 1105.46, 1051.46],
[ 120.23, 120.23, 120.41, 20.41, 120.23]]
new_list = []
for x, y in zip(l[0], l[1]):
new_list.append([x, y])
print(new_list)
输出:
[[1105.46, 120.23], [1105.75, 120.23], [1105.75, 120.41], [1105.46, 20.41], [1051.46, 120.23]]
具有列表理解的一行版本:
print([[x, y] for x, y in zip(l[0], l[1])])
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句