首页 > 解决方案 > 访问与每个元素关联的计数

问题描述

如果 a 有一个元素列表,其计数如下

[('a', 1), ('b', 2), ('c', 2),('d', 3), ('e', 3)]

现在我想知道其中有多少出现了一次、两次和三次。所以预期的输出会是这样的

1:1,2:2,3:2

列表由Counter函数生成Counter(lst)

标签: pythonlistcounter

解决方案


在你的第一个Counter上使用另一个。 valuesCounter

from collections import Counter

s = 'abbccdddeee'
c = Counter(s)
counts = Counter(c.values())

推荐阅读