【发布时间】:2016-05-20 03:07:54
【问题描述】:
我一直在研究这个问题,
用于解决 'combining' multiple Counter().object 或 dicts;但仍然无法完成。
我在下面找到了两个参考:
- Is there any pythonic way to combine two dicts (adding values for keys that appear in both)?
- Summing the contents of two collections.Counter() objects
例如,我有很多“计数器类型”变量名,顺序为[1 to 100]:
Name:counter_1 Value:Counter({'a':1, 'b':2, 'c':3})
Name:counter_2 Value:Counter({'b':5, 'c':19, f:17})
Name:counter_3 Value:Counter({'a':11, 'b':22, 'c':33, 'd':97})
...
Name:counter_100 Value:Counter({'c':55, 'd':22, 'e':63, 'f':21})
如果我手动添加每个counter_1 + counter_2 + ... + counter_3,那会让我发疯。
有没有更优雅或更简单的方法来sum()?
谢谢一百万!
【问题讨论】:
标签: python dictionary sum counter