collections模块 ==> Python标准库,数据结构常用的模块;collections包含了一些特殊的容器,针对Python内置的容器,例如list、dict、set和tuple,提供了另一种选择。

collections模块常用类型有:

计数器(

Counter

)

dict的子类,计算可hash的对象

请点击

Counter

双向队列(

deque

)

类似于list的容器,可以快速的在队列头部和尾部添加、删除元素

请点击

deque

默认字典(defaultdict)

dict的子类,可以调用提供默认值的函数

有序字典(OrderedDict)

dict的子类,可以记住元素的添加顺序

可命名元组(namedtuple)

可以创建包含名称的tuple

Counter()

主要功能:可以支持方便、快速的计数,将元素数量统计,然后计数并返回一个字典,键为元素,值为元素个数。

from collections import Counter

list1 = ["a", "a", "a", "b", "c", "c", "f", "g", "g", "g", "f"]

dic = Counter(list1)

print(dic)

#结果:次数是从高到低的

#Counter({'a': 3, 'g': 3, 'c': 2, 'f': 2, 'b': 1})

print(dict(dic))

#结果:按字母顺序排序的

#{'a': 3, 'b': 1, 'c': 2, 'f': 2, 'g': 3}

print(dic.items()) #dic.items()获取字典的key和value

#结果:按字母顺序排序的

#dict_items([('a', 3), ('b', 1), ('c', 2), ('f', 2), ('g', 3)])

print(dic.keys())

#结果:

#dict_keys(['a', 'b', 'c', 'f', 'g'])

print(dic.values())

#结果:

#dict_values([3, 1, 2, 2, 3])

print(sorted(dic.items(), key=lambda s: (-s[1])))

#结果:按统计次数降序排序

#[('a', 3), ('g', 3), ('c', 2), ('f', 2), ('b', 1)]

for i, v in dic.items():

if v == 1:

print(i)

#结果:

#b

from collections import Counter

str1 = "aabbfkrigbgsejaae"

print(Counter(str1))

print(dict(Counter(str1)))

#结果:

#Counter({'a': 4, 'b': 3, 'g': 2, 'e': 2, 'f': 1, 'k': 1, 'r': 1, 'i': 1, 's': 1, 'j': 1})

#{'a': 4, 'b': 3, 'f': 1, 'k': 1, 'r': 1, 'i': 1, 'g': 2, 's': 1, 'e': 2, 'j': 1}

dic1 = {'a': 3, 'b': 4, 'c': 0, 'd': -2}

print(Counter(dic1))

Counter对象支持以下三个字典不支持的方法,update()字典支持

most_common()

返回一个

列表

,包含counter中n个最大数目的元素,如果忽略n或者为None,most_common()将会返回counter中的所有元素,元素有着相同数目的将会选择出现早的元素

list1 = ["a", "a", "a", "b", "c", "f", "g", "g", "c", "11", "g", "f", "10", "2"]

print(Counter(list1).most_common(3))

#结果:[('a', 3), ('g', 3), ('c', 2)]

#"c"、"f"调换位置,结果变化

list2 = ["a", "a", "a", "b", "f", "c", "g", "g", "c", "11", "g", "f", "10", "2"]

print(Counter(list2).most_common(3))

#结果:[('a', 3), ('g', 3), ('f', 2)]

elements()

返回一个

迭代器

,每个元素重复的次数为它的数目,顺序是任意的顺序,如果一个元素的数目少于1,那么elements()就会忽略它

list2 = ["a", "a", "abdz", "abc", "f", "c", "g", "g", "c", "c1a1", "g", "f", "111000", "b10"]

print(''.join(Counter(list2).elements()))

#结果:aaabdzabcffccgggc1a1111000b10

print(''.join(list2))

#结果:aaabdzabcfcggcc1a1gf111000b10

dic1 = {'a': 3, 'b': 4, 'c': 0, 'd': -2, "e": 0}

print(Counter(dic1))

print(list(Counter(dic1).elements()))

#结果:

#Counter({'b': 4, 'a': 3, 'c': 0, 'e': 0, 'd': -2})

#['a', 'a', 'a', 'b', 'b', 'b', 'b']

update()

从一个可迭代对象(可迭代对象是一个元素序列,而非(key,value)对构成的序列)中或者另一个映射(或counter)中所有元素相加,是数目相加而非替换它们

dic1 = {'a': 3, 'b': 4, 'c': 0, 'd': -2, "e": 0}

dic2 = {'a': 3, 'b': 4, 'c': 0, 'd': 2, "e": -1, "f": 6}

a = Counter(dic1)

print(a)

#结果:Counter({'b': 4, 'a': 3, 'c': 0, 'e': 0, 'd': -2})

b = Counter(dic2)

print(b)

#结果:Counter({'f': 6, 'b': 4, 'a': 3, 'd': 2, 'c': 0, 'e': -1})

a.update(b)

print(a)

#结果:Counter({'b': 8, 'a': 6, 'f': 6, 'c': 0, 'd': 0, 'e': -1})

subtract()

从一个可迭代对象中或者另一个映射(或counter)中,元素相减,是数目相减而不是替换它们

dic1 = {'a': 3, 'b': 4, 'c': 0, 'd': -2, "e": 0}

dic2 = {'a': 3, 'b': 4, 'c': 0, 'd': 2, "e": -1, "f": 6}

a = Counter(dic1)

print(a)

#结果:Counter({'b': 4, 'a': 3, 'c': 0, 'e': 0, 'd': -2})

b = Counter(dic2)

print(b)

#结果:Counter({'f': 6, 'b': 4, 'a': 3, 'd': 2, 'c': 0, 'e': -1})

a.subtract(b)

print(a)

#结果:Counter({'e': 1, 'a': 0, 'b': 0, 'c': 0, 'd': -4, 'f': -6})

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

  1. python collection counter_python collection模块中几种数据结构(Counter、OrderedDict、namedtup)详解...

    collection模块中有几种数据结构我们可能用得到. Counter是字典的子类,负责计数的一个字典,支持 + 加法 - 减法 & 求公共元素 | 求并集 print('Counter类型 ...

  2. python中collections_Python中的collections模块

    Python中内置了4种数据类型,包括:list,tuple,set,dict,这些数据类型都有其各自的特点,但是这些特点(比如dict无序)在一定程度上对数据类型的使用产生了约束,在某些使用场景下效 ...

  3. Python的collections模块中namedtuple结构使用示例

    namedtuple顾名思义,就是名字+元组的数据结构,下面就来看一下Python的collections模块中namedtuple结构使用示例 namedtuple 就是命名的 tuple,比较像 ...

  4. python3异步task_并发,异步编程_Python中的asyncio模块中的Future和Task的区别?,并发,异步编程,python,asyncio - phpStudy...

    Python中的asyncio模块中的Future和Task的区别? 问题一 按照官方文档的描述,Task是Futrue的一个subclass,标准库中也分别提供了create_task和create ...

  5. python里的collections模块

    python里的collections模块 collections模块里提供了一些特殊功能的容器: namedtuple deque ChainMap Counter OrderedDict defa ...

  6. python内置collections模块的使用

    python内置collections模块的使用 文章目录: 一.collections模块说明 1.查看collections模块的定义路径 2.查看collections文档介绍信息 3.查看co ...

  7. python中search和match的区别_Python中正则表达式match()、search()函数及match()和search()的区别详解...

    match()和search()都是python中的正则匹配函数,那这两个函数有何区别呢? match()函数只检测RE是不是在string的开始位置匹配, search()会扫描整个string查找 ...

  8. 作为SLAM中最常用的闭环检测方法,视觉词袋模型技术详解来了

    摘自:https://mp.weixin.qq.com/s/OZnnuA31tEaVt0vnDOy5hQ 作为SLAM中最常用的闭环检测方法,视觉词袋模型技术详解来了 原创 小翼 飞思实验室 今天 基 ...

  9. linux上传文件命令ftp put,Linux ftp 命令行中下载文件get与上传文件put的命令应用详解...

    介绍:从本地以用户anok登录的机器192.168.0.16上通过ftp远程登录到192.168.0.6的ftp服务器上,登录用户名是peo.以下为使用该连接做的实验. 查看远程ftp服务器上用户pe ...

最新文章

  1. [转]如果我有jQuery背景,我应该如何切换到AngularJS的思维模式?
  2. ABAP--通过LDB_PROCESS函数使用逻辑数据库
  3. C语言运算符优先级 详细列表
  4. linux week3
  5. React Native在Android当中实践(五)——常见问题
  6. js 运行中断停止_javascript 终止函数执行操作
  7. Mysql配置项sync_binlog=0
  8. 蒙特卡洛模拟Ising模型
  9. python微信机器人
  10. php公文流转管理系统,OA办公系统公文流转
  11. 已知二叉树的前序序列跟中序序列求后序序列(C语言)
  12. ECS主动运维事件--让你HOLD住全场 (二)
  13. 【夏栀的博客】3月9日零点正式上线
  14. 学测试,看视频?NONONO,除非这种情况
  15. 软帝出品2019阿里面试题大全(含答案解析)
  16. 中国神话中的诸神辈分如何排
  17. centos7 ipv4配置
  18. 基于STM32的智能温室控制系统仿真电路设计(温控补光)-基于STM32的智能蓝牙温控风扇控制系统设计-基于STM32的无线蓝牙心电监护仪系统设计【毕设课设分享】
  19. 软件体系结构——层次风格
  20. 苹果手机连接电脑没反应

热门文章

  1. 小马智行Pony.ai 2020校招宣讲行程来了!
  2. Nature论文解读 | 基于深度学习和心脏影像预测生存概率
  3. 论文解读:深度监督网络(Deeply-Supervised Nets)
  4. Ⅶ:教你一招利用zookeeper作为服务的配置中心
  5. spring-注入集合对象
  6. uni-app——map组件路线[polyline]功能示例
  7. Spring Session——@EnableSpringHttpSession注解
  8. TensorFlow 教程——手写数字识别
  9. JavaFX——JavaFX概览
  10. CG CTF WEB COOKIE