collections系列之Counter
collections模块中有一个叫做Counter的类,该类的作用就是计数器,Counter是对dict的加工,所有Counter继承了dict的方法
1、创建一个Counter,需要import collections库,通过collections库创建Counter,主要的功能就是统计每个元素出现的次数
import collections
obj = collections.Counter("adalfjklajflkajfklejlasdkfjaklfjlkasfja")
print(obj)
# Counter({'a': 8, 'l': 7, 'j': 7, 'f': 6, 'k': 6, 'd': 2, 's': 2, 'e': 1})
l1 = ["ada","ada","cc"]
obj = collections.Counter(l1)
print(obj)
# Counter({'ada': 2, 'cc': 1})
2、Counter的另外一个方法就是most_common,可以指定统计出出现次数排名前X的元素,出现的次数从大到小排列,比如下面的例子,只统计出现次数最多的前3个元素
obj = collections.Counter("adalfjklajflkajfklejlasdkfjaklfjlkasfja")
print(obj)
ret = obj.most_common(3)
print(ret)
# [('a', 8), ('l', 7), ('j', 7)]
3、elements方法,可以列出Counter对象中的每一个元素
l1 = ["ada","ada","cc"]
obj = collections.Counter(l1)
print(obj)
for i in obj.elements():
print(i) # ada
# ada
# cc
4、update方法,可以增加Counter对象中的内容
l1 = ["ada","ada","cc"]
obj = collections.Counter(l1)
print(obj)
# Counter({'ada': 2, 'cc': 1})
l2 =["cc","add","ada",1]
obj.update(l2)
print(obj)
# Counter({'ada': 3, 'cc': 2, 'add': 1, 1: 1})
5、subtract方法,可以减少Counter对象中的内容,可以看到如果减少的元素在Counter中没有,则该元素的值为-1
l1 = ["ada","ada","cc"]
l2 =["cc","add","ada",1]
obj = collections.Counter(l1)
print(obj)
# Counter({'ada': 2, 'cc': 1})
obj.subtract(l2)
print(obj)
# Counter({'ada': 1, 'cc': 0, 'add': -1, 1: -1})
collections系列之Counter的更多相关文章
- Python 第三篇(下):collections系列、集合(set)、单双队列、深浅copy、内置函数
一.collections系列: collections其实是python的标准库,也就是python的一个内置模块,因此使用之前导入一下collections模块即可,collections在py ...
- python递归、collections系列以及文件操作进阶
global log 127.0.0.1 local2 daemon maxconn log 127.0.0.1 local2 info defaults log global mode http t ...
- Python之set集合与collections系列
1>set集合:是一个无序且不重复的元素集合:访问速度快,解决了重复的问题: s2 = set(["che","liu","haha" ...
- python之路(二)-collections系列
collections提供了一些比较方便的方法,使用时需要先导入模块 导入模块: import collections 1. 计数器Counter 统计参数中出现的次数,以字典的方式返回结果,参数可以 ...
- python3 开发面试题(collections中的Counter)6.7
''' 编写Python脚本,分析xx.log文件,按域名统计访问次数 xx.log文件内容如下: https://www.sogo.com/ale.html https://www.qq.com/3 ...
- collections系列之OrderedDict【有序字典】与DefaultDict【默认字典】
今天来向大家介绍一下collections系列中的OrderedDict和DefaultDict,这两种类均是通过collections来创建的,均是对dict字典加工,所有都继承了dict字典的方法 ...
- Python标准库——collections模块的Counter类
1.collections模块 collections模块自Python 2.4版本开始被引入,包含了dict.set.list.tuple以外的一些特殊的容器类型,分别是: OrderedDict类 ...
- Python中模块之collections系列
collection系列功能介绍 1. 常用的集中类 1. Counter(计数器) 计数器的常用方法如下: 创建一个字典计数器 格式:collections.Counter(obj) 例如:prin ...
- collections系列
一.计数器(counter) Counter是对字典类型的补充,用于追踪值的出现次数. ps:具备字典的所有功能 + 自己的功能 c = Counter('abcdeabcdabcaba') prin ...
随机推荐
- ubuntu 查看系统配置
1, 主板信息 .查看主板的序列号 -------------------------------------------------- #使用 命令 dmidecode | grep -i 'ser ...
- Tomcat 支持的Java 版本和兼容性总结
https://tomcat.apache.org/whichversion.html 最新最全的Tomcat 支持的Java版本对照,即兼容性一览表: Servlet Spec JSP Spec ...
- 字符串,数组,定时器,form
一.字符串 <script> /* // 字符串的属性 var a="hello world"; console.log(a.length); console.log( ...
- c++官方文档
来自官方文档...感谢老王指出需要c++11,一下代码全在c++11下编译,编译参数加入 -std=c++11 #include<stdio.h> #include<iostrea ...
- 给Jquery easyui 的datagrid 每行增加操作链接(转)
http://www.thinkphp.cn/code/207.html 通过formatter方法给Jquery easyui 的datagrid 每行增加操作链接我们都知道Jquery的EasyU ...
- Eclipse安装STS(Spring Tool Suite (STS) for Eclipse)插件
转自:https://blog.csdn.net/zhen_6137/article/details/79383941
- XCode iOS Simulator 模拟器
XCode7.3下,默认带了iOS 9.3 Simulator,iOS 8.4 Simulator总是安装不成功. mac os X,里的模拟器,全屏 ,windows win键+1/2/3 切换全屏 ...
- ADODB 调用存储过程
追加参数法调用存储过程 追加参数通过CreateParameter方法,用来指定属性创建新的Parameter对象.具体语法如下: Set parameter = command.CreatePara ...
- TensorFlow入门-Tianic数据集训练
import pandas as pd import tensorflow as tf from sklearn.model_selection import train_test_split imp ...
- 使用Linux命令行测试网速-----speedtest-cli
https://github.com/sivel/speedtest-cli 当发现上网速度变慢时,人们通常会先首先测试自己的电脑到网络服务提供商(通常被称为“最后一公里”)的网络连接速度.在可用于测 ...