一、计数器(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. Android——UI事件的处理机制(基于监听器)

    1.普通内部类(常用) xml <Button android:hint="reset" android:layout_columnWeight="1" ...

  2. awk "sort -rnk3"

    [root@Cobbler logs]# awk 'BEGIN{print "IP地址","访问流量","访问次数"}{a[$1]++;b[ ...

  3. 使ie9以下版本支持canvas,css3等主流html5技术的方法

    1.前言.   ie6,7,8支持html5,看起来比较难,其实有一种方法很通用,就是引入js和css,这种可插拔的引入对开发很有帮助.比如,下面是一个让网页支持canvas和css3的例子. 2.例 ...

  4. adb shell error: more than one device and emulator

    adb shell error: more than one device and emulator 本文转载出处: http://blog.sina.com.cn/s/blog_7ffb8dd501 ...

  5. 在函数体的“出口处”,对 return 语句的正确性和效率进行检查

    在函数体的“出口处”,对 return 语句的正确性和效率进行检查. 如果函数有返回值,那么函数的“出口处”是 return 语句. 我们不要轻视 return 语 句.如果 return 语句写得不 ...

  6. 【BZOJ】1052: [HAOI2007]覆盖问题(贪心)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1052 首先膜拜题解orz,表示只能想到二分... 贪心就是每一次找到一个最小的能包围所有点的矩阵,然 ...

  7. hdu 4709:Herding(叉积求三角形面积+枚举)

    Herding Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  8. MyBatis 3.2.6插入时候获取自增主键方法

    MyBatis 3.2.6插入时候获取自增主键方法有二 以MySQL5.5为例: 方法1: <insert id="insert" parameterType="P ...

  9. Mac终端Screen命令使用指南

    (1)创建会话 使用命令“screen -S RunWork”来创建一个screen会话,命令执行之后,就会得到一个新的shell窗口,为了便于标示可以用快捷键Ctrl-a A(就是按下Ctrl+a键 ...

  10. C++ STL迭代器与索引相互转换

    0 前言 C++ STL提供了vector.list等模板容器,极大地方便了编程使用. “遍历”是对容器使用的最常用的操作. 使用迭代器来遍历是最好最高效的遍历方法. 当然,对于有些容器的遍历除了使用 ...