python - 访问与每个元素关联的计数
问题描述
如果 a 有一个元素列表,其计数如下
[('a', 1), ('b', 2), ('c', 2),('d', 3), ('e', 3)]
现在我想知道其中有多少出现了一次、两次和三次。所以预期的输出会是这样的
1:1,2:2,3:2
列表由Counter
函数生成Counter(lst)
解决方案
在你的第一个Counter
上使用另一个。 values
Counter
from collections import Counter
s = 'abbccdddeee'
c = Counter(s)
counts = Counter(c.values())
推荐阅读
- c - 欧拉计划 - 问题 22:答案降低 3.35%
- c# - 如果给定函数参数而不是文字,实体框架和 Oracle,Linq where 子句字符串比较不返回任何记录
- java - Lombok 和 builder 注释
- javascript - 将动画添加到下拉菜单
- excel - 如何修复“运行时错误 1004,对象“_Application”的方法“宽度”失败”
- javascript - 显示完全错误图标的材料设计图标
- amazon-web-services - 使用 AWS-SDK 访问 AWS SSO
- java - Util 从字节数组中提取位到一个新的字节[]
- php - 如何将变量循环到sql调用中
- java - Java - 如何根据多个属性删除 ArrayList 中的重复项