简体   繁体   English

在Python中压缩嵌套列表

[英]Zipping nested lists in Python

Say I have the following two lists/numpy arrays: 说我有以下两个列表/ numpy数组:

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

I would like to obtain a list that holds the zipping of the nested lists above: 我想获得一个列表,其中包含上面嵌套列表的压缩:

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

Is there a way to do this with a one-liner (and in a Pythonic way)? 有没有办法用单线程(以Pythonic方式)做到这一点?

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)]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM