In [17]: from collections import Counter
In [18]: sum((Counter(d) for d in data.values()), Counter())
Out[18]: Counter({'C': 12, 'B': 7, 'A': 4, 'D': 3})
请注意,根据python文档,Counter仅为正值的用例设计:
The multiset methods are designed only for use cases with positive values. The inputs may be negative or zero, but only outputs with positive values are created. There are no type restrictions, but the value type needs to support addition, subtraction, and comparison.
The elements() method requires integer counts. It ignores zero and negative counts.
您可以使用
collections.Counter()
(仅适用于正值!)公司名称:请注意,根据python文档,
Counter
仅为正值的用例设计:所以,如果你想得到一个综合的结果,你可以手工求和。
^{pr2}$collections.defaultdict()
是解决此问题的好方法:试试这个
结果
^{pr2}$相关问题 更多 >
编程相关推荐