python学习——Counter计数方法
来源:互联网 发布:淘宝店铺改名字 编辑:程序博客网 时间:2024/06/07 01:25
1、统计一个序列中元素的次数:
from collections import Countera = [10, 8, 6, 7, 2, 8, 4, 10, 3, 7, 8, 4, 5, 7, 2, 2, 3, 8, 8, 9, 6, 2, 2, 7, 8, 7, 4, 8, 5, 2]b = Counter(a)print(b)
得到结果:
Counter({8: 7, 2: 6, 7: 5, 4: 3, 10: 2, 6: 2, 3: 2, 5: 2, 9: 1})
2、如果想要得到排名前几的话:
from collections import Countera = [10, 8, 6, 7, 2, 8, 4, 10, 3, 7, 8, 4, 5, 7, 2, 2, 3, 8, 8, 9, 6, 2, 2, 7, 8, 7, 4, 8, 5, 2]b = Counter(a).most_common(3)print(b)
得到结果:
[(8, 7), (2, 6), (7, 5)]
3、总结
(1)从collections集合模块中引入集合类Counter
(2)Counter(a)可以打印出数组a中每个元素出现的次数
(3)Counter(a).most_common(2)可以打印出数组中出现次数最多的元素。参数2表示的含义是:输出几个出现次数最多的元素。
阅读全文
0 0
- python学习——Counter计数方法
- python学习:counter计数
- Python Counter()计数工具
- python-Counter计数函数
- Python Counter()计数工具
- Python中Counter计数统计
- python使用Collections.Counter进行计数
- python 基础 —— collections.Counter()
- Python:使用Counter进行计数统计及collections模块
- 处理器 性能监控计数寄存器——Performance Monitor Counter registers
- Python标准库——collections模块的Counter类
- Python标准库(2)——数据结构之Counter
- python小技巧——enumerate,operator和Counter
- Python标准库——collections模块的Counter类
- Python标准库——collections模块的Counter类
- Python标准库——collections模块的Counter类
- Python标准库——collections模块的Counter类
- Python标准库——collections模块的Counter类
- 1054. 求平均值 (20)
- IntelliJ Idea 常用快捷键列表
- 将两个字符串或一个字符串与数组传入一个,输出字符串中需要的字符位置
- 1055. 集体照 (25)
- Markdown编辑数学公式的方法
- python学习——Counter计数方法
- C语言(结构体,共用体,教务系统项目的编写)
- C++ 模板用法
- JavaScript学习笔记整理(七)
- CodeForces
- 在Linux下怎样让top命令启动之后就按内存使用排序(或CPU使用排序)?
- VMware12+ubuntu16+c270 linux上使用摄像头
- 双链表的尾插法建立输出
- android进阶-surfaceView的分析和使用