Python中怎样高效可持续地统计个数

可以用set和字典推导式试一试,我不知道性能如何。\u0026gt;\u0026gt;\u0026gt; dict1 = {\u0026#39;a\u0026#39;:3}\u0026gt;\u0026gt;\u0026gt; dict2 = {\u0026#39;b\u0026#39;:4,\u0026#39;a\u0026#39;:2}\u0026gt;\u0026gt;\u0026gt; d = set(dict1) | set(dict2)\u0026gt;\u0026gt;\u0026gt; {k:dict1.get(k,0)+dict2.get(k,0) for k in d}{\u0026#39;a\u0026#39;: 5, \u0026#39;b\u0026#39;: 4}
■网友
【Python中怎样高效可持续地统计个数】 collections.Counter(itertools.chain(*X))


    推荐阅读