[英]How to merge lists of tuples element-wise?
I have two lists of tuples:我有两个元组列表:
x = [(1,2),(3,4),(5,6)]
y = [(7,),(8,),(9)]
And the desired output is所需的输出是
[(1,2,7),(3,4,8),(5,6,9)]
How should I proceed?我应该如何进行? I tried list(zip(x,y))
but it gives [((1,2),(7,)), ...
, which is not what I want.我试过list(zip(x,y))
但它给出了[((1,2),(7,)), ...
,这不是我想要的。
压缩它们,然后用列表推导式将它们连接起来:
[a + b for (a, b) in zip(x, y)]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.