0

0

Python 计数器:如何使用 collections.Counter?

王林

王林

发布时间:2023-05-08 13:34:07

|

1418人浏览过

|

来源于亿速云

转载

    一. 介绍

    一个计数器工具提供快速和方便的计数,counter是一个dict的子类,用于计数可哈希对象。它是一个集合,元素像字典键(key)一样存储,它们的计数存储为值。计数可以是任何整数值,包括0和负数,counter类有点像其他语言中的bags或multisets。简单说,就是可以统计计数,来几个例子看看就清楚了。
    举例:

    #计算top10的单词
    from collections import Counter
    import re
    text = 'remove an existing key one level down remove an existing key one level down'
    words = re.findall(r'\w+', text)
    Counter(words).most_common(10)
    [('remove', 2),('an', 2),('existing', 2),('key', 2),('one', 2)('level', 2),('down', 2)] 
    
    
    #计算列表中单词的个数
    cnt = Counter()
    for word in ['red', 'blue', 'red', 'green', 'blue', 'blue']:
        cnt[word] += 1
    cnt
    Counter({'red': 2, 'blue': 3, 'green': 1})
    
    
    #上述这样计算有点嘛,下面的方法更简单,直接计算就行
    L = ['red', 'blue', 'red', 'green', 'blue', 'blue'] 
    Counter(L)
    Counter({'red': 2, 'blue': 3, 'green': 1}

    元素从一个iterable 被计数或从其他的mapping (or counter)初始化:

    from collections import Counter
    
    #字符串计数
    Counter('gallahad') 
    Counter({'g': 1, 'a': 3, 'l': 2, 'h': 1, 'd': 1})
    
    #字典计数
    Counter({'red': 4, 'blue': 2})  
    Counter({'red': 4, 'blue': 2})
    
    #计数
    Counter(cats=4, dogs=8)
    Counter({'cats': 4, 'dogs': 8})
    
    Counter(['red', 'blue', 'red', 'green', 'blue', 'blue'])
    Counter({'red': 2, 'blue': 3, 'green': 1})

    二. 基本操作

    1. 统计“可迭代序列”中每个元素的出现的次数

    1.1 对列表/字符串作用

    下面是两种使用方法,一种是直接使用,一种是实例化以后使用,如果要频繁调用的话,显然后一种更简洁 ,因为可以方便地调用Counter内的各种方法,对于其他可迭代序列也是一样的套路。

    #首先引入该方法
    from collections import Counter
    #对列表作用
    list_01 = [1,9,9,5,0,8,0,9]  #GNZ48-陈珂生日
    print(Counter(list_01))  #Counter({9: 3, 0: 2, 1: 1, 5: 1, 8: 1})
     
    #对字符串作用
    temp = Counter('abcdeabcdabcaba')
    print(temp)  #Counter({'a': 5, 'b': 4, 'c': 3, 'd': 2, 'e': 1})
    #以上其实是两种使用方法,一种是直接用,一种是实例化以后使用,如果要频繁调用的话,显然后一种更简洁

    1.2 输出结果

    立即学习Python免费学习笔记(深入)”;

    #查看类型
    print( type(temp) ) #
     
    #转换为字典后输出
    print( dict(temp) ) #{'b': 4, 'a': 5, 'c': 3, 'd': 2, 'e': 1}
     
    for num,count in enumerate(dict(temp).items()):
        print(count)
    """
    ('e', 1)
    ('c', 3)
    ('a', 5)
    ('b', 4)
    ('d', 2)
    """

    1.3 用自带的items()方法输出

    显然这个方法比转换为字典后再输出的方法更为方便:

    print(temp.items()) #dict_items([('e', 1), ('c', 3), ('b', 4), ('d', 2), ('a', 5)])
     
    for item in temp.items():
        print(item)
    """
    ('a', 5)
    ('c', 3)
    ('d', 2)
    ('e', 1)
    ('b', 4)
    """

    2. most_common()统计出现次数最多的元素

    利用most_common()方法,返回一个列表,其中包含n个最常见的元素及出现次数,按常见程度由高到低排序。 如果 n 被省略或为None,most_common() 将返回计数器中的所有元素,计数值相等的元素按首次出现的顺序排序,经常用来计算top词频的词语:

    #求序列中出现次数最多的元素
     
    from collections import Counter
     
    list_01 = [1,9,9,5,0,8,0,9]
    temp = Counter(list_01)
     
    #统计出现次数最多的一个元素
    print(temp.most_common(1))   #[(9, 3)]  元素“9”出现3次。
    print(temp.most_common(2)) #[(9, 3), (0, 2)]  统计出现次数最多个两个元素
     
    #没有指定个数,就列出全部
    print(temp.most_common())  #[(9, 3), (0, 2), (1, 1), (5, 1), (8, 1)]
    Counter('abracadabra').most_common(3)
    [('a', 5), ('b', 2), ('r', 2)]
    
    Counter('abracadabra').most_common(5)
    [('a', 5), ('b', 2), ('r', 2), ('c', 1), ('d', 1)]

    3. elements() 和 sort()方法

    描述:返回一个迭代器,其中每个元素将重复出现计数值所指定次。 元素会按首次出现的顺序返回。 如果一个元素的计数值小于1,elements() 将会忽略它。
    举例:

    c = Counter(a=4, b=2, c=0, d=-2)
    list(c.elements())
    ['a', 'a', 'a', 'a', 'b', 'b']
    
    sorted(c.elements())
    ['a', 'a', 'a', 'a', 'b', 'b']
    
    c = Counter(a=4, b=2, c=0, d=5)
    list(c.elements())
    ['a', 'a', 'a', 'a', 'b', 'b', 'd', 'd', 'd', 'd', 'd']
    from collections import Counter
     
    c = Counter('ABCABCCC')
    print(c.elements()) #
     
    #尝试转换为list
    print(list(c.elements())) #['A', 'A', 'C', 'C', 'C', 'C', 'B', 'B']
     
    #或者这种方式
    print(sorted(c.elements()))  #['A', 'A', 'B', 'B', 'C', 'C', 'C', 'C']
     
    #这里与sorted的作用是: list all unique elements,列出所有唯一元素
    #例如
    print( sorted(c) ) #['A', 'B', 'C']

    官方文档例子:

    # Knuth's example for prime factors of 1836:  2**2 * 3**3 * 17**1
    prime_factors = Counter({2: 2, 3: 3, 17: 1})
    product = 1
    for factor in prime_factors.elements():  # loop over factors
        product *= factor  # and multiply them
    print(product)  #1836
    #1836 = 2*2*3*3*3*17

    4. subtract()减操作:输出不会忽略掉结果为零或者小于零的计数

    从迭代对象或映射对象减去元素,输入和输出都可以是0或者负数。

    c = Counter(a=4, b=2, c=0, d=-2)
    d = Counter(a=1, b=2, c=3, d=4)
    c.subtract(d)
    c
    Counter({'a': 3, 'b': 0, 'c': -3, 'd': -6})
    
    #减去一个abcd
    str0 = Counter('aabbccdde')
    str0
    Counter({'a': 2, 'b': 2, 'c': 2, 'd': 2, 'e': 1})
    
    str0.subtract('abcd')
    str0
    Counter({'a': 1, 'b': 1, 'c': 1, 'd': 1, 'e': 1}
    subtract_test01 = Counter("AAB")
    subtract_test01.subtract("BCC")
    print(subtract_test01)  #Counter({'A': 2, 'B': 0, 'C': -2})

    这里的计数可以减到零一下,可以包含零和负数:

    subtract_test02 = Counter("which")
    subtract_test02.subtract("witch")  #从另一个迭代序列中减去元素
    subtract_test02.subtract(Counter("watch"))  #^……
     
    #查看结果
    print( subtract_test02["h"] )  # 0 ,whirch 中两个,减去witch中一个,减去watch中一个,剩0个
    print( subtract_test02["w"] )  #-1

    5. 字典方法

    通常字典方法都可用于Counter对象,除了有两个方法工作方式与字典并不相同。

    • fromkeys(iterable):这个类方法没有在Counter中实现。

      站长俱乐部购物系统
      站长俱乐部购物系统

      功能介绍:1、模块化的程序设计,使得前台页面设计与程序设计几乎完全分离。在前台页面采用过程调用方法。在修改页面设计时只需要在相应位置调用设计好的过程就可以了。另外,这些过程还提供了不同的调用参数,以实现不同的效果;2、阅读等级功能,可以加密产品,进行收费管理;3、可以完全可视化编辑文章内容,所见即所得;4、无组件上传文件,服务器无需安装任何上传组件,无需支持FSO,即可上传文件。可限制文件上传的类

      下载
    • update([iterable-or-mapping]):从迭代对象计数元素或者从另一个映射对象 (或计数器) 添加,元素个数是加上。另外迭代对象应该是序列元素,而不是一个 (key, value) 对。

    sum(c.values())                 # total of all counts
    c.clear()                       # reset all counts
    list(c)                         # list unique elements
    set(c)                          # convert to a set
    dict(c)                         # convert to a regular dictionary
    c.items()                       # convert to a list of (elem, cnt) pairs
    Counter(dict(list_of_pairs))    # convert from a list of (elem, cnt) pairs
    c.most_common(n)                   # n least common elements
    +c                              # remove zero and negative counts

    6. 数学操作

    这个功能非常强大,提供了几个数学操作,可以结合 Counter 对象,以生产 multisets (计数器中大于0的元素)。 加和减,结合计数器,通过加上或者减去元素的相应计数。交集和并集返回相应计数的最小或最大值。每种操作都可以接受带符号的计数,但是输出会忽略掉结果为零或者小于零的计数。

    c = Counter(a=3, b=1)
    d = Counter(a=1, b=2)
    c + d                       # add two counters together:  c[x] + d[x]
    Counter({'a': 4, 'b': 3})
    c - d                       # subtract (keeping only positive counts)
    Counter({'a': 2})
    c & d                       # intersection:  min(c[x], d[x]) 
    Counter({'a': 1, 'b': 1})
    c | d                       # union:  max(c[x], d[x])
    Counter({'a': 3, 'b': 2})
    print(Counter('AAB') + Counter('BCC'))
    #Counter({'B': 2, 'C': 2, 'A': 2})
    print(Counter("AAB")-Counter("BCC"))
    #Counter({'A': 2})

    与”和“或”操作:

    print(Counter('AAB') & Counter('BBCC'))
    #Counter({'B': 1})
     
    print(Counter('AAB') | Counter('BBCC'))
    #Counter({'A': 2, 'C': 2, 'B': 2})

    单目加和减(一元操作符)意思是从空计数器加或者减去,相当于给计数值乘以正值或负值,同样输出会忽略掉结果为零或者小于零的计数:

    c = Counter(a=2, b=-4)
    +c
    Counter({'a': 2})
    -c
    Counter({'b': 4})

    写一个计算文本相似的算法,加权相似:

    def str_sim(str_0,str_1,topn):
        topn = int(topn)
        collect0 = Counter(dict(Counter(str_0).most_common(topn)))
        collect1 = Counter(dict(Counter(str_1).most_common(topn)))       
        jiao = collect0 & collect1
        bing = collect0 | collect1       
        sim = float(sum(jiao.values()))/float(sum(bing.values()))        
        return(sim)         
    
    str_0 = '定位手机定位汽车定位GPS定位人定位位置查询'         
    str_1 = '导航定位手机定位汽车定位GPS定位人定位位置查询'         
    
    str_sim(str_0,str_1,5)    
    0.75

    7. 计算元素总数、Keys() 和 Values()

    from collections import Counter
     
    c = Counter('ABCABCCC')
    print(sum(c.values()))  # 8  total of all counts
     
    print(c.keys())  #dict_keys(['A', 'B', 'C'])
    print(c.values())  #dict_values([2, 2, 4])

    8. 查询单元素结果

    from collections import Counter
    c = Counter('ABBCC')
    #查询具体某个元素的个数
    print(c["A"])  #1

    9. 添加

    for elem in 'ADD':  # update counts from an iterabl
        c[elem] += 1
    print(c.most_common())  #[('C', 2), ('D', 2), ('A', 2), ('B', 2)]
    #可以看出“A”增加了一个,新增了两个“D”

    10. 删除(del)

    del c["D"]
    print(c.most_common())  #[('C', 2), ('A', 2), ('B', 2)]
    del c["C"]
    print(c.most_common())  #[('A', 2), ('B', 2)]

    11. 更新 update()

    d = Counter("CCDD")
    c.update(d)
    print(c.most_common())  #[('B', 2), ('A', 2), ('C', 2), ('D', 2)]

    12. 清除 clear()

    c.clear()
    print(c)  #Counter()

    三. 总结

    Counter是一个dict子类,主要是用来对你访问的对象的频率进行计数。

    常用方法:

    • elements():返回一个迭代器,每个元素重复计算的个数,如果一个元素的计数小于1,就会被忽略。

    • most_common([n]):返回一个列表,提供n个访问频率最高的元素和计数

    • subtract([iterable-or-mapping]):从迭代对象中减去元素,输入输出可以是0或者负数,不同于减号 - 的作用

    • update([iterable-or-mapping]):从迭代对象计数元素或者从另一个 映射对象 (或计数器) 添加。

    举例:

    # 统计字符出现的次数
    >>> import collections
    >>> collections.Counter('hello world')
    Counter({'l': 3, 'o': 2, 'h': 1, 'e': 1, ' ': 1, 'w': 1, 'r': 1, 'd': 1})
    # 统计单词数
    >>> collections.Counter('hello world hello world hello nihao'.split())
    Counter({'hello': 3, 'world': 2, 'nihao': 1})

    常用的方法:

    >>> c = collections.Counter('hello world hello world hello nihao'.split())
    >>> c
    Counter({'hello': 3, 'world': 2, 'nihao': 1})
    # 获取指定对象的访问次数,也可以使用get()方法
    >>> c['hello']
    3
    >>> c = collections.Counter('hello world hello world hello nihao'.split())
    # 查看元素
    >>> list(c.elements())
    ['hello', 'hello', 'hello', 'world', 'world', 'nihao']
    # 追加对象,或者使用c.update(d)
    >>> c = collections.Counter('hello world hello world hello nihao'.split())
    >>> d = collections.Counter('hello world'.split())
    >>> c
    Counter({'hello': 3, 'world': 2, 'nihao': 1})
    >>> d
    Counter({'hello': 1, 'world': 1})
    >>> c + d
    Counter({'hello': 4, 'world': 3, 'nihao': 1})
    # 减少对象,或者使用c.subtract(d)
    >>> c - d
    Counter({'hello': 2, 'world': 1, 'nihao': 1})
    # 清除
    >>> c.clear()
    >>> c
    Counter()

    相关文章

    python速学教程(入门到精通)
    python速学教程(入门到精通)

    python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

    下载

    相关标签:

    本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

    相关专题

    更多
    云朵浏览器入口合集
    云朵浏览器入口合集

    本专题整合了云朵浏览器入口合集,阅读专题下面的文章了解更多详细地址。

    0

    2026.01.20

    Java JVM 原理与性能调优实战
    Java JVM 原理与性能调优实战

    本专题系统讲解 Java 虚拟机(JVM)的核心工作原理与性能调优方法,包括 JVM 内存结构、对象创建与回收流程、垃圾回收器(Serial、CMS、G1、ZGC)对比分析、常见内存泄漏与性能瓶颈排查,以及 JVM 参数调优与监控工具(jstat、jmap、jvisualvm)的实战使用。通过真实案例,帮助学习者掌握 Java 应用在生产环境中的性能分析与优化能力。

    20

    2026.01.20

    PS使用蒙版相关教程
    PS使用蒙版相关教程

    本专题整合了ps使用蒙版相关教程,阅读专题下面的文章了解更多详细内容。

    62

    2026.01.19

    java用途介绍
    java用途介绍

    本专题整合了java用途功能相关介绍,阅读专题下面的文章了解更多详细内容。

    87

    2026.01.19

    java输出数组相关教程
    java输出数组相关教程

    本专题整合了java输出数组相关教程,阅读专题下面的文章了解更多详细内容。

    39

    2026.01.19

    java接口相关教程
    java接口相关教程

    本专题整合了java接口相关内容,阅读专题下面的文章了解更多详细内容。

    10

    2026.01.19

    xml格式相关教程
    xml格式相关教程

    本专题整合了xml格式相关教程汇总,阅读专题下面的文章了解更多详细内容。

    13

    2026.01.19

    PHP WebSocket 实时通信开发
    PHP WebSocket 实时通信开发

    本专题系统讲解 PHP 在实时通信与长连接场景中的应用实践,涵盖 WebSocket 协议原理、服务端连接管理、消息推送机制、心跳检测、断线重连以及与前端的实时交互实现。通过聊天系统、实时通知等案例,帮助开发者掌握 使用 PHP 构建实时通信与推送服务的完整开发流程,适用于即时消息与高互动性应用场景。

    19

    2026.01.19

    微信聊天记录删除恢复导出教程汇总
    微信聊天记录删除恢复导出教程汇总

    本专题整合了微信聊天记录相关教程大全,阅读专题下面的文章了解更多详细内容。

    160

    2026.01.18

    热门下载

    更多
    网站特效
    /
    网站源码
    /
    网站素材
    /
    前端模板

    精品课程

    更多
    相关推荐
    /
    热门推荐
    /
    最新课程
    最新Python教程 从入门到精通
    最新Python教程 从入门到精通

    共4课时 | 7.3万人学习

    Django 教程
    Django 教程

    共28课时 | 3.3万人学习

    SciPy 教程
    SciPy 教程

    共10课时 | 1.2万人学习

    关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
    php中文网:公益在线php培训,帮助PHP学习者快速成长!
    关注服务号 技术交流群
    PHP中文网订阅号
    每天精选资源文章推送

    Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号