Counter字典的子类,用于统计哈希对象

from collections import Counter

users = ["body1","body11","body13","body14","body11","body12","body13","body11"]

user_counter = Counter(users)

print (user_counter)    

#打印结果:
Counter({'body11': 3, 'body13': 2, 'body1': 1, 'body14': 1, 'body12': 1}) user1_counter = Counter("abcdABCDAAccDd")    #统计字符串
print (user1_counter)

#打印结果:
Counter({'c': 3, 'A': 3, 'd': 2, 'D': 2, 'a': 1, 'b': 1, 'B': 1, 'C': 1})
user1_counter.update(user_counter)    #更新合并统计(将user_counter和user1_counter的数据合并统计)
print (user1_counter)

#打印结果:
Counter({'c': 3, 'A': 3, 'body11': 3, 'd': 2, 'D': 2, 'body13': 2, 'a': 1, 'b': 1, 'B': 1, 'C': 1, 'body1': 1, 'body14': 1, 'body12': 1})
user1_counter.update('')       #合并统计(将user1_counter的数据加入1234后统计)
print (user1_counter)

打印结果:
Counter({'c': 3, 'A': 3, 'body11': 3, 'd': 2, 'D': 2, 'body13': 2, 'a': 1, 'b': 1, 'B': 1, 'C': 1, 'body1': 1, 'body14': 1, 'body12': 1, '1': 1, '2': 1, '3': 1, '4': 1})
#统计出出现次数最多的前n个元素,示例打印前两个
print(user_counter.most_common(2)) #打印结果:
[('body11', 3), ('body13', 2)]

键的删除

>>> from collections import Counter
>>> c = Counter("abcdcba")
>>> c
Counter({'a': 2, 'b': 2, 'c': 2, 'd': 1})
>>> del c["a"]
>>> c
Counter({'b': 2, 'c': 2, 'd': 1})
>>>

算术和集合操作

+、-、&、| 操作也可以用于Counter。其中&和|操作分别返回两个Counter对象各元素的最小值和最大值。需要注意的是,得到的Counter对象将删除小于1的元素。

>>> c = Counter(a=3, b=1)
>>> d = Counter(a=1, b=2)
>>> c + d # c[x] + d[x]
Counter({'a': 4, 'b': 3})
>>> c - d # subtract(只保留正数计数的元素)
Counter({'a': 2})
>>> c & d # 交集: min(c[x], d[x])
Counter({'a': 1, 'b': 1})
>>> c | d # 并集: max(c[x], d[x])
Counter({'a': 3, 'b': 2})

其它操作

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-1个元素
c += Counter() # 移除0和负值

collections 模块之Counter的更多相关文章

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

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

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

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

  3. python的Collections 模块

    Collections 模块 知识点 Counter 类 defaultdict 类 namedtuple 类 在这个实验我们会学习 Collections 模块.这个模块实现了一些很好的数据结构,它 ...

  4. python:collections模块

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

  5. collections 模块(namedtuple, deque, Counter )

    基本介绍 我们都知道,Python拥有一些内置的数据类型,比如str, int, list, tuple, dict等, collections模块在这些内置数据类型的基础上,提供了几个额外的数据类型 ...

  6. collections模块---(namedtuple、deque、OrderdDict、defaultdict、Counter)和configparser模块

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

  7. python collections模块 计数器(counter)

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

  8. python collections模块

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

  9. Python collections 模块用法举例

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

随机推荐

  1. BZOJ2084[Poi2010]Antisymmetry——回文自动机

    题目描述 对于一个01字符串,如果将这个字符串0和1取反后,再将整个串反过来和原串一样,就称作“反对称”字符串.比如00001111和010101就是反对称的,1001就不是.现在给出一个长度为N的0 ...

  2. 网页调起QQ聊天

    将QQ账号换成正常的QQ号即可,要确保这个QQ支持临时会话 <a href="http://wpa.qq.com/msgrd?v=3&uin=QQ账号&site=qq& ...

  3. CodeForces512C-Pluses everywhere-模拟/数学/排列组合模板

    经过研究可以发现,每一位的贡献是C(n-2,k-1)+C(n-3,k-1)...C(k-1,k-1) 同时还要注意加号全部在左边的情况. 这里还用了O(n)预处理O(1)组合数的模板.//妙啊..妙. ...

  4. Minimum number of steps CodeForces - 805D(签到题)

    D. Minimum number of steps time limit per test 1 second memory limit per test 256 megabytes input st ...

  5. Android studio 3.1.2报错,no target device found

     Android studio 3.1.2的Android monitor改为Android profiler,直接点这个就可以真机调试,在手机安装相应文件 第二次启动时,再次报错,找不到设备,点一下 ...

  6. MT【169】拉格朗日配方

    已知$x^2+y^2+z^2=1$求$3xy-3yz+2z^2$的最大值______ 答案:$3$ 提示:$3(x^2+y^2+z^2)-(3xy-3yz+2z^2)=3\left(y+\dfrac{ ...

  7. 牛客练习赛40 A 小D的剧场 (思维dp)

    链接:https://ac.nowcoder.com/acm/contest/369/A 题目描述 若你摘得小的星星 你将得到小的幸福  若你摘得大的星星 你将得到大的财富  若两者都能摘得 你将得到 ...

  8. This license xxx has been cancelled 解决

    上节回顾:JetBrains全家桶破解思路 hosts屏蔽一下即可,Linux是:/etc/hosts 0.0.0.0 account.jetbrains.com 重新输入Code即可,最后补一个地址 ...

  9. bzoj1218 激光炸弹

    bz上难得一见的水题啊. 我们发现:这个SB居然只要枚举就行了!!! 我TM...... /****************************************************** ...

  10. HDU3032 Nim or not Nim?

    解:使用sg函数打表发现规律,然后暴力异或起来即可. #include <bits/stdc++.h> typedef long long LL; ; int a[N]; inline L ...