虽然如果可能的话我也更喜欢使用Pandas,但这里有一个使用纯Python的解决方案:
In [1]: import itertools
In [2]: dictionary =[{'Flow': 100, 'Location': 'USA', 'Name': 'A1'},
...: {'Flow': 90, 'Location': 'Europe', 'Name': 'B1'},
...: {'Flow': 20, 'Location': 'USA', 'Name': 'A1'},
...: {'Flow': 70, 'Location': 'Europe', 'Name': 'B1'}]
...:
In [3]: import operator
In [4]: key = operator.itemgetter('Location', 'Name')
In [5]: [{'Flow': sum(x['Flow'] for x in g),
...: 'Location': k[0],
...: 'Name': k[1]}
...: for k, g in itertools.groupby(sorted(dictionary, key=key), key=key)]
...:
...:
Out[5]:
[{'Flow': 160, 'Location': 'Europe', 'Name': 'B1'},
{'Flow': 120, 'Location': 'USA', 'Name': 'A1'}]
另一种方法是使用defaultdict,它会给你一个稍微不同的表示(虽然如果你想的话,可以将其转换回字典列表):
In [11]: import collections
In [12]: cnt = collections.defaultdict(int)
In [13]: for r in dictionary:
...: cnt[(r['Location'], r['Name'])] += r['Flow']
...:
In [14]: cnt
Out[14]: defaultdict(int, {('Europe', 'B1'): 160, ('USA', 'A1'): 120})
In [15]: [{'Flow': x, 'Location': k[0], 'Name': k[1]} for k, x in cnt.items()]
Out[15]:
[{'Flow': 120, 'Location': 'USA', 'Name': 'A1'},
{'Flow': 160, 'Location': 'Europe', 'Name': 'B1'}]
**{'Flow': sum(map(itemgetter('Flow'), g))}
简化为'Flow': sum(map(itemgetter('Flow'), g))
) - Aran-Fey