输入:
[["US", 2], ["UK", 3], ["FR", 4], ["US", 2], ["US", 2], ["UK", 2]]
输出:
[["US", 6], ["UK", 5], ["FR", 4]]
如果列表中的第一个元素匹配,我想要对列表的第二个元素求和。我尝试过使用字典和集合,但我无法想出逻辑。这可以在Hadoop或Spark中轻松完成,因为框架将采用reduce部分,我们可以轻松地对值列表求和。但我不知道如何在python中做。有人可以帮忙吗?
注意:我正在寻找优化的解决方案。没有使用很多for循环。
import collections
l1 = [["US", 2], ["UK", 3], ["FR", 4]]
l2 = [["US", "us@mail.com"], ["UK", "uk@mail.com"], ["BR", "fr@mail.com"]]
l1 = dict(l1)
l2 = dict(l2)
l1set = set(l1.keys())
l2set = set(l2.keys())
for i in l1set & l2set:
print l2[i]
答案 0 :(得分:0)
import collections as co
l = [["US", 2], ["UK", 3], ["FR", 4], ["US", 2], ["US", 2], ["UK", 2]]
dd = co.defaultdict(int)
for i in l:
dd[i[0]] += i[1]
newlist = [list((k,v)) for k,v in dd.iteritems()]
结果:
>>> newlist
[['FR', 4], ['UK', 5], ['US', 6]]
编辑:
如果您可以使用pandas,请按照https://stackoverflow.com/a/38497749/42346执行以下操作:
import pandas as pd
newlist = [list((k,v)) for k,v in pd.DataFrame(l,columns=['a','b']).groupby('a').b.sum().to_dict().iteritems()]
结果:
>>> newlist
[['FR', 4], ['US', 6], ['UK', 5]]
答案 1 :(得分:0)
列表理解:
myNewList = [i for i in listOne if i in listTwo]
以下是一个例子:
listOne = [2, 4, 5, 7]
listTwo = [2, 3, 5, 6]
print ([i for i in listOne if i in listTwo])
# prints [2, 5]
这是我用两个列表运行时得到的结果:
$ python test.py
[['FR', 4]]
答案 2 :(得分:0)
首先,如果你不知道怎么做,你就不需要优化,但我会给自己一个5秒的挑战来回答你的问题:)
from collections import defaultdict
b=defaultdict(int)
a=[["US", 2], ["UK", 3], ["FR", 4], ["US", 2], ["US", 2], ["UK", 2]]
for i in a: b[i[0]]+=i[1]
#now the way you access your sum is
print b['UK'] #prints 5
#if you specificlly need that format
output = [[n,b[n]] for n in b]
答案 3 :(得分:0)
您可以使用itertools.groupby
,reduce
和列表推导的组合,如下所示:
a = [["US", 2], ["UK", 3], ["FR", 4], ["US", 2], ["US", 2], ["UK", 2]]
a.sort()
b = []
for k, g in groupby(a, lambda x: x[0]):
b.append([k, reduce(lambda p, c: p + c, [y[1] for y in g])])
答案 4 :(得分:0)
按名称分组,将每组的数字相加:
from itertools import groupby
from operator import itemgetter
my_list = [["US", 2], ["UK", 3], ["FR", 4], ["US", 2], ["US", 2], ["UK", 2]]
summary_list = []
for name, group in groupby(sorted(my_list), key=itemgetter(0)):
summary_list.append([name, sum(item[1] for item in group)])
print(summary_list)
输出:
Python 3.5.1 (default, Dec 2015, 13:05:11)
[GCC 4.8.2] on linux
[['FR', 4], ['UK', 5], ['US', 6]]
答案 5 :(得分:0)
从这开始:
ll =[["US", 2], ["UK", 3], ["FR", 4], ["US", 2], ["US", 2], ["UK", 2]]
试试这个:
dd = {k:0 for k in dict(ll).keys()}
for x in ll: dd[x[0]] += x[1]
dd
{'FR': 4, 'UK': 5, 'US': 6}
[[k,v] for k,v in dd.iteritems()]
[['FR', 4], ['US', 6], ['UK', 5]]