2

假设我有以下两个列表/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 方式)做到这一点?

4

1 回答 1

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)]]
于 2012-12-03T00:17:22.433 回答