简单掌握Python的Collections模块中counter结构的用法


Posted in Python onJuly 07, 2016

counter 是一种特殊的字典,主要方便用来计数,key 是要计数的 item,value 保存的是个数。

from collections import Counter

>>> c = Counter('hello,world')
Counter({'l': 3, 'o': 2, 'e': 1, 'd': 1, 'h': 1, ',': 1, 'r': 1, 'w': 1})

初始化可以传入三种类型的参数:字典,其他 iterable 的数据类型,还有命名的参数对。

| __init__(self, iterable=None, **kwds)
 |  Create a new, empty Counter object. And if given, count elements
 |  from an input iterable. Or, initialize the count from another mapping
 |  of elements to their counts.
 |
 |  >>> c = Counter()       # a new, empty counter
 |  >>> c = Counter('gallahad')     # a new counter from an iterable
 |  >>> c = Counter({'a': 4, 'b': 2})   # a new counter from a mapping
 |  >>> c = Counter(a=4, b=2)     # a new counter from keyword args

默认请求下,访问不存在的 item,会返回 0。Counter 可以用来统计某些数据的出现次数,比如一个很长的数字串 numbers = "67642192097348921647512014651027586741512651" 中每个数字的频率:

>>> c = Counter(numbers) # c 存储了每个数字的频率
>>> c.most_common()  # 所有数字按照频率排序。如果 most_common 接受了 int 参数 n,将返回频率前n 的数据,否则会返回所有的数据
[('1', 8),
 ('2', 6),
 ('6', 6),
 ('5', 5),
 ('4', 5),
 ('7', 5),
 ('0', 3),
 ('9', 3),
 ('8', 2),
 ('3', 1)]

此外,你还可以对两个 Counter 对象进行 +, -,min, max 等操作。

综合示例:

print('Counter类型的应用') 
c = Counter("dengjingdong") 
#c = Counter({'n': 3, 'g': 3, 'd': 2, 'i': 1, 'o': 1, 'e': 1, 'j': 1}) 
print("原始数据:",c) 
print("最多的两个元素:",c.most_common(2))#输出数量最多的元素 
print("d的个数:",c['d'])#输出d的个数 
print(c.values())#输出字典的value列表 
print(sum(c.values()))#输出总字符数 
print(sorted(c.elements()))#将字典中的数据,按字典序排序 
print('\n\n') 
""" 
#删除所有d元素 
del c['d'] 
b = Counter("dengxiaoxiao") 
#通过subtract函数删除元素,元素个数可以变成负数。 
c.subtract(b) 
""" 
 
""" 
可以添加数据 
b = Counter("qinghuabeida") 
c.update(b) 
"""
Python 相关文章推荐
Python简单进程锁代码实例
Apr 27 Python
【Python】Python的urllib模块、urllib2模块批量进行网页下载文件
Nov 19 Python
Python正则表达式实现简易计算器功能示例
May 07 Python
浅谈python3中input输入的使用
Aug 02 Python
python 爬取学信网登录页面的例子
Aug 13 Python
关于pandas的离散化,面元划分详解
Nov 22 Python
tensorflow 实现自定义梯度反向传播代码
Feb 10 Python
Python开发之pip安装及使用方法详解
Feb 21 Python
Python新建项目自动添加介绍和utf-8编码的方法
Dec 26 Python
python 模块导入问题汇总
Feb 01 Python
python 如何用map()函数创建多线程任务
Apr 07 Python
Python实现猜拳与猜数字游戏的方法详解
Apr 06 Python
Python处理json字符串转化为字典的简单实现
Jul 07 #Python
全面了解python字符串和字典
Jul 07 #Python
对于Python中RawString的理解介绍
Jul 07 #Python
python变量不能以数字打头详解
Jul 06 #Python
Python中shutil模块的常用文件操作函数用法示例
Jul 05 #Python
详解Python中的array数组模块相关使用
Jul 05 #Python
简单掌握Python中glob模块查找文件路径的用法
Jul 05 #Python
You might like
第十三节 对象串行化 [13]
2006/10/09 PHP
php下intval()和(int)转换使用与区别
2008/07/18 PHP
php curl 伪造IP来源的实例代码
2012/11/01 PHP
php程序总是提示验证码输入有误解决方案
2015/01/07 PHP
thinkphp5.1 文件引入路径问题及注意事项
2018/06/13 PHP
JS维吉尼亚密码算法实现代码
2010/11/09 Javascript
javascript常用代码段搜集
2014/12/04 Javascript
浅谈JavaScript Date日期和时间对象
2014/12/29 Javascript
JavaScript中用字面量创建对象介绍
2014/12/31 Javascript
JavaScript实现DIV层拖动及动态增加新层的方法
2015/05/12 Javascript
微信js-sdk上传与下载图片接口用法示例
2016/10/12 Javascript
利用浮层使select不可选的实现方法
2016/12/03 Javascript
利用node.js本地搭建HTTP服务器
2017/04/19 Javascript
Javascript实现的StopWatch功能示例
2017/06/13 Javascript
JS原生轮播图的简单实现(推荐)
2017/07/22 Javascript
IntersectionObserver实现图片懒加载的示例
2017/09/29 Javascript
NodeJS 中Stream 的基本使用
2018/07/30 NodeJs
NodeJS实现一个聊天室功能
2019/11/25 NodeJs
python sys模块sys.path使用方法示例
2013/12/04 Python
使用Python生成随机密码的示例分享
2016/02/18 Python
理解生产者消费者模型及在Python编程中的运用实例
2016/06/26 Python
Python使用functools模块中的partial函数生成偏函数
2016/07/02 Python
python链接oracle数据库以及数据库的增删改查实例
2018/01/30 Python
基于pandas数据样本行列选取的方法
2018/04/20 Python
基于pip install django失败时的解决方法
2018/06/12 Python
pygame游戏之旅 添加游戏界面按键图形
2018/11/20 Python
详解python校验SQL脚本命名规则
2019/03/22 Python
Python + Requests + Unittest接口自动化测试实例分析
2019/12/12 Python
flask框架自定义url转换器操作详解
2020/01/25 Python
手工制作的意大利礼服鞋:Ace Marks
2018/12/15 全球购物
含精油的天然有机化妆品:Indemne
2019/08/27 全球购物
Yummie官方网站:塑身衣和衣柜必需品
2019/10/29 全球购物
淘宝网店营销策划书
2014/01/11 职场文书
工程资料员岗位职责
2014/03/10 职场文书
生活部的活动方案
2014/08/19 职场文书
600字作文之感受大自然
2019/11/27 职场文书