一、计数器(counter)

Counter是对字典类型的补充,用于追踪值的出现次数。

ps:具备字典的所有功能 + 自己的功能

把我写入的元素出现的多少次都计算出来
import collections

# 创建一个Counter对象
obj = collections.Counter('ddccbbqqaaa')
print(obj) '''
把我写入的元素出现的多少次都计算出来
Counter({'a': 3, 'd': 2, 'c': 2, 'b': 2, 'q': 2}) '''
obj.most_common() 
要取多少得值
import collections

# 创建一个Counter对象
obj = collections.Counter('ddccbbqqaaa')
print(obj) '''
把我写入的元素出现的多少次都计算出来
Counter({'a': 3, 'd': 2, 'c': 2, 'b': 2, 'q': 2}) ''' ret = obj.most_common(4)
print(ret) # 取前4位
# [('a', 3), ('d', 2), ('c', 2), ('b', 2)]

循环字典

import collections

# 创建一个Counter对象
obj = collections.Counter('ddccbbqqaaa')
print(obj) # Counter({'a': 3, 'd': 2, 'c': 2, 'b': 2, 'q': 2}) # 循环 字典 for i in obj:
print(i,obj[i]) '''
d 2
c 2
b 2
q 2
a 3
'''
elements() 传的是原生的值
import collections

# 创建一个Counter对象
obj = collections.Counter("dd") # elements() 传的是原生的值
print(list(obj.elements())) # ['d', 'd'] obj = collections.Counter(['','','','']) print(list(obj.elements()))
# ['1', '2', '3', '4']
update() 更新计数器 增加
在原来基础上添加
import collections

# 创建一个Counter对象

obj = collections.Counter(['','','',''])

print(obj)
# Counter({'22': 2, '11': 1, '44': 1}) # update() 更新计数器
obj.update(["","","eric"])
print(obj)
# Counter({'11': 3, '22': 2, '44': 1, 'eric': 1})
subtract()
删除
原来的计数器中的每一个元素的数量减去后添加的元素的数量
 
import collections

# 创建一个Counter对象

obj = collections.Counter(['','','',''])

print(obj)
# Counter({'22': 2, '11': 1, '44': 1}) # update() 更新计数器
obj.update(["","","eric"])
print(obj)
# Counter({'11': 3, '22': 2, '44': 1, 'eric': 1}) obj.subtract(['eric'])
print(obj) # Counter({'11': 3, '22': 2, '44': 1, 'eric': 0})
import collections

# 创建一个Counter对象

obj = collections.Counter(['','','',''])

print(obj)
# Counter({'22': 2, '11': 1, '44': 1}) obj.subtract(['eric','','',''])
print(obj) # Counter({'11': 1, '44': 1, '22': -1, 'eric': -1})

python collections模块 计数器(counter)的更多相关文章

  1. Python标准库——collections模块的Counter类

    1.collections模块 collections模块自Python 2.4版本开始被引入,包含了dict.set.list.tuple以外的一些特殊的容器类型,分别是: OrderedDict类 ...

  2. Python collections模块总结

    Python collections模块总结 除了我们使用的那些基础的数据结构,还有包括其它的一些模块提供的数据结构,有时甚至比基础的数据结构还要好用. collections ChainMap 这是 ...

  3. (转)python collections模块详解

    python collections模块详解 原文:http://www.cnblogs.com/dahu-daqing/p/7040490.html 1.模块简介 collections包含了一些特 ...

  4. Python中Collections模块的Counter容器类使用教程

    1.collections模块 collections模块自Python 2.4版本开始被引入,包含了dict.set.list.tuple以外的一些特殊的容器类型,分别是: OrderedDict类 ...

  5. Python collections 模块用法举例

    Python作为一个“内置电池”的编程语言,标准库里面拥有非常多好用的模块.比如今天想给大家 介绍的 collections 就是一个非常好的例子. 1.collections模块基本介绍 我们都知道 ...

  6. python collections模块

    collections模块基本介绍 collections在通用的容器dict,list,set和tuple之上提供了几个可选的数据类型 namedtuple() factory function f ...

  7. Python——collections模块

    collections模块 collections模块在内置数据类型(dict.list.set.tuple)的基础上,还提供了几个额外的数据类型:ChainMap.Counter.deque.def ...

  8. Python——collections模块、time模块、random模块、os模块、sys模块

    1. collections模块 (1)namedtuple # (1)点的坐标 from collections import namedtuple Point = namedtuple('poin ...

  9. python collections模块详解

    参考老顽童博客,他写的很详细,例子也很容易操作和理解. 1.模块简介 collections包含了一些特殊的容器,针对Python内置的容器,例如list.dict.set和tuple,提供了另一种选 ...

随机推荐

  1. [网络]Linux一些网络知识

    今天刚搬到新家,ubuntu一启动,无线网络又连不上了,之前就是大费周折才搞好的,于是又花了两小时才搞好. 下面就先来了解一些基础知识: 1. ifconfig输出的eth0/lo/wlan0分别代表 ...

  2. Spider Studio 界面功能布局

    SS是Spider Studio (采集工作站) 的简称, 这是由GDT团队开发的一款互联网数据采集开发工具. 它以浏览器为基础, 运用JQuery技术, 结合脚本化C#的强大功能, 能够轻松解决各类 ...

  3. Java运行结果测试

  4. arduino知识

    如果你买过Adafruit, Sparkfun, Seeedstudio和Arduino Store的Kit,不难发现Arduino官方的无论从包装和印刷上都是最具艺术气质的,其次是Sparkfun, ...

  5. JavaScript的gzip静态压缩方法记录

    传统的JS压缩(删除注释,删除多余空格等)提供的压缩率有时还是不尽不意,幸亏现在的浏览器都支持压缩传输(通过设置http header的Content-Encoding=gzip),可以通过服务器的配 ...

  6. 第二百七十节,Tornado框架-生成验证码图片,以及验证码结合Session验证

    Tornado框架-生成验证码图片,以及验证码结合Session验证 第一.生成验证码图片  生成验证码图片需要两个必须模块 1.python自带的random(随机模块) 2.Pillow()图像处 ...

  7. 【BZOJ】1088: [SCOI2005]扫雷Mine(递推)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1088 脑残去想递推去了... 对于每一个第二列的格子,考虑多种情况,然后转移.....QAQ 空间可 ...

  8. 解决 Failure to transfer * from http://repo1.maven.org/maven2

    解决 Failure to transfer * from http://repo1.maven.org/maven2 Failure to transfer org.apache.maven:mav ...

  9. H&M

    H&M于1947年由Erling Persson在瑞典创立.如今,H&M在全世界1500 多个专卖店销售服装.配饰与化妆品.位于瑞典市Stora Gatan大街的老H&M店是世 ...

  10. hdu 1358:Period(KMP算法,next[]数组的使用)

    Period Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...