您的数据结构应该真正移动到字典中,键入cat
值。使用collections.defaultdict()
和collections.Counter()
来跟踪值并使求和更容易:
from collections import defaultdict, Counter
totals = defaultdict(Counter)
for item in data:
cat = item.pop('cat')
totals[cat] += Counter(item)
演示:
>>> from collections import defaultdict, Counter
>>> data = {'cat':'red', 'a':1, 'b':2}, {'cat':'red', 'a':3, 'b':3}, {'cat':'blue', 'a':1, 'b':3}
>>> totals = defaultdict(Counter)
>>> for item in data:
... cat = item.pop('cat')
... totals[cat] += Counter(item)
...
>>> totals
defaultdict(<class 'collections.Counter'>, {'blue': Counter({'b': 3, 'a': 1}), 'red': Counter({'b': 5, 'a': 4})})
>>> totals['blue']
Counter({'b': 3, 'a': 1})
>>> totals['red']
Counter({'b': 5, 'a': 4})
如果您仍然需要相同格式的字典序列,则可以将上述计数器字典再次转换为“普通”字典:
output = []
for cat, counts in totals.iteritems():
item = {'cat': cat}
item.update(counts)
output.append(item)
导致:
>>> output
[{'a': 1, 'b': 3, 'cat': 'blue'}, {'a': 4, 'b': 5, 'cat': 'red'}]