简体   繁体   English

如何对两个元组列表求和

[英]How to sum two lists of tuples

list1 = [(1533945600000, 140), (1534032000000, 412), (1534118400000, 364), (1534204800000, 488), (1534291200000, 272), (1534377600000, 350), (1534464000000, 301), (1534550400000, 159), (1534636800000, 224), (1534723200000, 241), (1534809600000, 223), (1534896000000, 175)]


list2 = [(1533945600000, 1516), (1534032000000, 2176), (1534118400000, 2046), (1534204800000, 2400), (1534291200000, 8370), (1534377600000, 2112), (1534464000000, 1441), (1534550400000, 784), (1534636800000, 1391), (1534723200000, 1178), (1534809600000, 1020), (1534896000000, 795)]

How could you combine them into a single list of tuples?你怎么能把它们组合成一个元组列表? considering the first value of the key appears in both lists.考虑到键的第一个值出现在两个列表中。

use zip on list1,list2 and then add second value of each tuple:在 list1,list2 上使用zip ,然后添加每个元组的第二个值:

lst = []
for tup1,tup2 in zip(list1,list2):
    sum_ = tup1[1]+tup2[1]
    lst.append((tup1[0],sum_))

lst

[(1533945600000, 1656),
 (1534032000000, 2588),
 (1534118400000, 2410),
 (1534204800000, 2888),
 (1534291200000, 8642),
 (1534377600000, 2462),
 (1534464000000, 1742),
 (1534550400000, 943),
 (1534636800000, 1615),
 (1534723200000, 1419),
 (1534809600000, 1243),
 (1534896000000, 970)]

You can use the following snippet if the lists are not guaranteed to have the same length or the keys are not guaranteed to be present in both lists or they are not in the same order:如果不能保证列表具有相同的长度或不能保证键出现在两个列表中或它们的顺序不同,则可以使用以下代码段:

from collections import defaultdict

list1 = [(1533945600000, 140), (1534032000000, 412), (1534118400000, 364), (1534204800000, 488), (1534291200000, 272), (1534377600000, 350), (1534464000000, 301), (1534550400000, 159), (1534636800000, 224), (1534723200000, 241), (1534809600000, 223), (1534896000000, 175)]
list2 = [(1533945600000, 1516), (1534032000000, 2176), (1534118400000, 2046), (1534204800000, 2400), (1534291200000, 8370), (1534377600000, 2112), (1534464000000, 1441), (1534550400000, 784), (1534636800000, 1391), (1534723200000, 1178), (1534809600000, 1020), (1534896000000, 795)]

d = defaultdict(int, list1)
for key, n in list2:                   
    d[key] += n
res = list(map(tuple, d.items()))

res:资源:

[(1533945600000, 1656),
 (1534032000000, 2588),
 (1534118400000, 2410),
 (1534204800000, 2888),
 (1534291200000, 8642),
 (1534377600000, 2462),
 (1534464000000, 1742),
 (1534550400000, 943),
 (1534636800000, 1615),
 (1534723200000, 1419),
 (1534809600000, 1243),
 (1534896000000, 970)]

Using List comprehension with zip :zip使用List comprehension

>>> [(x[0], x[1]+y[1]) for x,y in zip(list1, list2)]

#driver values : #驱动程序值:

IN : list1 = [(1533945600000, 140), (1534032000000, 412), (1534118400000, 364), (1534204800000, 488), (1534291200000, 272), (1534377600000, 350), (1534464000000, 301), (1534550400000, 159), (1534636800000, 224), (1534723200000, 241), (1534809600000, 223), (1534896000000, 175)]
     list2 = [(1533945600000, 1516), (1534032000000, 2176), (1534118400000, 2046), (1534204800000, 2400), (1534291200000, 8370), (1534377600000, 2112), (1534464000000, 1441), (1534550400000, 784), (1534636800000, 1391), (1534723200000, 1178), (1534809600000, 1020), (1534896000000, 795)]

OUT : [(1533945600000, 1656), (1534032000000, 2588), (1534118400000, 2410), (1534204800000, 2888), (1534291200000, 8642), (1534377600000, 2462), (1534464000000, 1742), (1534550400000, 943), (1534636800000, 1615), (1534723200000, 1419), (1534809600000, 1243), (1534896000000, 970)]
combined_list=[]
for i,j in zip(list1,list2):
     combined_list.append((i[0]+j[0],i[1]+j[1]))

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

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