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

OrderedDict类:排序字典,是字典的子类。引入自2.7。
namedtuple()函数:命名元组,是一个工厂函数。引入自2.6。
Counter类:为hashable对象计数,是字典的子类。引入自2.7。
deque:双向队列。引入自2.4。
defaultdict:使用工厂函数创建字典,使不用考虑缺失的字典键。引入自2.5。

Counter类的目的是用来跟踪值出现的次数。它是一个无序的容器类型,以字典的键值对形式存储,其中元素作为key,其计数作为value。计数值可以是任意的Interger(包括0和负数)。

from collections import Counter
c1=Counter('abcdefgacdiabc')
print(c1['a'])  # 3
print(c1['p'])  # 0
c2=Counter('with')
c2.update('within')
print(c2['w'])  #2
print(c2['i'])  ## 减少则使用subtract()
c2.subtract('ww')
print(c2['w'])  # 0
# 键的删除 当计数值为0时,并不意味着元素被删除,删除元素应当使用del
c1['a'] = 0
print(c1)   # Counter({'c': 3, 'd': 2, 'b': 2, 'g': 1, 'e': 1, 'i': 1, 'f': 1, 'a': 0})
del c1['a']
print(c1)   # Counter({'c': 3, 'd': 2, 'b': 2, 'e': 1, 'i': 1, 'f': 1, 'g': 1})
# elements()
c3=Counter(a=4,b=2,c=1,d=0)
print(type(c3.elements()))  # <class 'itertools.chain'>
print(list(c3.elements()))  # ['c', 'a', 'a', 'a', 'a', 'b', 'b']
c4=Counter(a=4,b=2,c=1,d=-2)
print(list(c4.elements()))  # ['c', 'a', 'a', 'a', 'a', 'b', 'b']
#most_common([n]) 返回一个TopN列表。如果n没有被指定,则返回所有元素。当多个元素计数值相同时,排列是无确定顺序的。
print(c1.most_common()) # [('c', 3), ('b', 2), ('d', 2), ('f', 1), ('e', 1), ('i', 1), ('g', 1)]
print(c1.most_common(3))    # [('c', 3), ('d', 2), ('b', 2)]

Counter 常用操作

sum(c.values())  # 所有计数的总数
c.clear()  # 重置Counter对象,注意不是删除
list(c)  # 将c中的键转为列表
set(c)  # 将c中的键转为set
dict(c)  # 将c中的键值对转为字典
c.items()  # 转为(elem, cnt)格式的列表
Counter(dict(list_of_pairs))  # 从(elem, cnt)格式的列表转换为Counter类对象
c.most_common()[:-n:-1]  # 取出计数最少的n个元素
c += Counter()  # 移除0和负值

collections模块的Counter类相关推荐

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

    更多16 最近在看一本名叫<Python Algorithm: Mastering Basic Algorithms in the Python Language>的书,刚好看到提到这个C ...

  2. Python标准库collections模块的Counter类

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

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

    Python中Collections模块的Counter容器类使用教程 1.collections模块 collections模块自Python2.4版本开始被引入,包含了dict,set,list, ...

  4. python中counter_Python collections模块中counter()的详细说明,Pythoncollections,之,Counter,详解...

    collections模块 ==> Python标准库,数据结构常用的模块:collections包含了一些特殊的容器,针对Python内置的容器,例如list.dict.set和tuple,提 ...

  5. python中collections中的counter类_了解Python的collections.Counter类型

    python视频教程栏目介绍Python的collections.Counter类型. collections.Counter 类型可以用来给可散列的对象计数,或者是当成多重集合来使用 -- 多重集合 ...

  6. python counter模块_python collections模块 计数器(counter)

    一.计数器(counter) Counter是对字典类型的补充,用于追踪值的出现次数. ps:具备字典的所有功能 + 自己的功能 把我写入的元素出现的多少次都计算出来 import collectio ...

  7. python的collections模块的学习

    转发连接:https://www.cnblogs.com/mrchige/p/6379831.html 转发用于自己学习,并无其他商用 Collections 模块 知识点 Counter 类 def ...

  8. python︱ collections模块(namedtuple/defaultdict/OrderedDict等)

    collections有的功能: ['deque', 'defaultdict', 'namedtuple', 'UserDict', 'UserList', 'UserString',\'Count ...

  9. python:collections模块

    Counter类 介绍:A counter tool is provided to support convenient and rapid tallies 构造:class collections. ...

最新文章

  1. 红帽子linux开启telnet,RedHat linux 9下配置telnet服务 | 技术部落
  2. 《R语言数据分析》——3.2 聚集
  3. leetcode 575. 分糖果(Java版)
  4. 带有Angular JS的Java EE 7 – CRUD,REST,验证–第2部分
  5. java自动识别验证码_Java使用OCR技术识别验证码实现自动化登陆方法
  6. python3实用编程技巧_6.python3实用编程技巧进阶(一)
  7. ioca0中断 pic单片机_单片机入门知识-PIC为例程序.ppt
  8. java 阿里线程池_为什么阿里不允许使用 Executors 创建线程池?
  9. N76E003的学习之路(一)
  10. 【转】HTTP幂等性概念和应用
  11. 拇指接龙游戏中的Undo道具与STL容器deque简介
  12. Glide修改本地图片缓存路径
  13. 计算机系统结构自考知识点总结,自考《计算机系统结构》问答题总结(3)
  14. 恢复计算机个性化设置方法,Win7主题默认还原电脑用了主题软件没办法恢复完美解决系统主-win7主题...
  15. 联想换机助手_三星s换机助手下载-三星S换机助手 安卓版v3.6.07.11-pc6手机下载
  16. 基于STM32设计的NB-IOT电量采集系统(超级详细)--2.STM32连接M5311及HLW8032测试
  17. YOLOV5改进||YOLOV5+GSConv+Slim Neck
  18. Ext.ux.form.SearchField 添加placeholder属性 2016年9月19日
  19. 自动化的内容生成语言模型如何帮助您赢得seo竞赛
  20. 处理 __iob_func 无法链接的问题

热门文章

  1. 分布式事务实践 解决数据一致性 分布式系统
  2. 自适应网站设计对百度友好的关键:添加applicable-device标签(转)
  3. 线上故障快速定位及恢复
  4. win7 php环境搭建 x64,win7搭建php+Apache环境
  5. Java 线程池ThreadPoolExecutor的应用与源码解析
  6. Maven入门基础-环境篇
  7. 自动驾驶模拟器Carla之python编程-(1)简介
  8. linux makefile详解
  9. Laravel核心解读--中间件(Middleware)
  10. arduino neo 定位不可用_arduino霹雳七彩灯