[英]How to + the values in two lists of tuples
How can I add the tuples from two lists of tuples to get a new list of the results? 如何从两个元组列表中添加元组以获取结果的新列表?
For example: 例如:
a = [(1,1),(2,2),(3,3)]
b = [(1,1),(2,2),(3,3)]
We want to get 我们想得到
c = [(2,2),(4,4),(6,6)]
I searched google and found many results how to simply add two lists together using zip, but could not find anything about two lists of tuples. 我搜索谷歌并发现许多结果如何使用zip简单地添加两个列表,但找不到有关两个元组列表的任何内容。
use zip
twice and a list comprehension: 使用
zip
两次和列表理解:
In [63]: a = [(1,1),(2,2),(3,3)]
In [64]: b = [(1,1),(2,2),(3,3)]
In [66]: [tuple(map(sum, zip(x, y))) for x, y in zip(a, b)]
Out[66]: [(2, 2), (4, 4), (6, 6)]
>>> a = [(1,1),(2,2),(3,3)]
>>> b = [(1,1),(2,2),(3,3)]
>>> [(i[0]+j[0], i[1]+j[1]) for i, j in zip(a,b)]
[(2, 2), (4, 4), (6, 6)]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.