在Python中压缩嵌套列表

时间:2012-12-03 00:15:28

标签: python

说我有以下两个列表/ numpy数组:

List1 = [[1,2,3,4], [10,11,12], ...]
List2 = [[-1,-2-3,-4], [-10,-11,-12], ...]

我想获得一个包含上面嵌套列表的压缩列表:

Result = [[(1,-1), (2,-2), (3,-3), (4,-4)], [(10,-10), (11, -11), (12,-12)], ...]

有没有办法用单线程(并以Pythonic方式)执行此操作?

1 个答案:

答案 0 :(得分:7)

l1 = [[1,2,3,4], [10,11,12]]
l2 = [[-1,-2,-3,-4], [-10,-11,-12]]

print [zip(a,b) for a,b in zip(l1,l2)]
[[(1, -1), (2, -2), (3, -3), (4, -4)], [(10, -10), (11, -11), (12, -12)]]