如何对两个元组列表求和

时间:2018-08-23 10:38:37

标签: python

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

如何将它们合并为一个元组列表?考虑到密钥的第一个值出现在两个列表中。

4 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:2)

List comprehensionzip结合使用:

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

#driver值:

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

答案 2 :(得分:1)

如果不能保证列表具有相同的长度,或者不能保证键在两个列表中都存在,或者它们的顺序不相同,则可以使用以下代码段:

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

答案 3 :(得分:1)

combined_list=[]
for i,j in zip(list1,list2):
     combined_list.append((i[0]+j[0],i[1]+j[1]))