美文网首页
collections.Counter的使用

collections.Counter的使用

作者: 转身丶即天涯 | 来源:发表于2019-07-21 22:01 被阅读0次

    这是一个统计一个序列中元素出现的次数的内置库。


    统计字符串

    如果我们有这样一个字符串。我们要统计字符串中每个字符出现的次数,可以使用Counter类来轻松搞定。

    my_str = "hello world. python is the best."
    
    from collections import Counter
    
    s = "hello world. python is the best."
    c = Counter(s)
    
    print(c)
    

    输出如下:


    image.png

    然后我们可以方便的做进一步处理。比如,统计's' 't' 'a' 'c' 'k'所有字符总共出现了几次?

    from collections import Counter
    
    s = "hello world. python is the best."
    c = Counter(s)
    
    total = sum([v for k, v in c.items() if k in ['s', 't', 'a', 'c', 'k']])
    print(total)
    

    思路:遍历Counter对象c,我们可以简单认为c是一个dict,使用items()方法可以遍历dict的key和value。
    如果key是’stack‘这五个字符中的任意一个,则把value作为一个列表元素返回到列表。
    最后调用sum()函数,对列表生成式进行求和。


    接下来试试list

    再比如,我们现在有一个名字列表,我们要统计每个名字出现的次数。

    from collections import Counter
    
    elements = ['jack', 'JACK', 'lucy', 'April', 'Lucy', 'mike', 'JAck']
    c = Counter(elements)
    print(c)
    

    输出如下图:


    image.png

    Counter类的特性

    1. 通过前面的例子,你也许已经猜到了,Counter()的构造函数接受一个序列(str, list, tuple, set)作为参数。

    2. 可以使用counter.update()更新counter

    from collections import Counter
    
    elements = ['jack', 'JACK', 'lucy', 'April', 'Lucy', 'mike', 'JAck']
    
    c = Counter(elements)
    
    print(c)
    
    new_elements = ['alice', 'steven']
    c.update(new_elements)
    
    print(c)
    
    image.png
    1. 可以使用counter.subtract()函数减少counter
    from collections import Counter
    
    elements = ['jack', 'JACK', 'lucy', 'April', 'Lucy', 'mike', 'JAck']
    
    c = Counter(elements)
    
    print(c)
    
    new_elements = ['alice', 'steven', 'jack']
    c.subtract(new_elements)
    
    print(c)
    
    image.png

    通过结果,我们发现如果new_elements中的元素如果存在会被减1,如果原来的list中没有这个元素,这个元素会被记为-1.

    1. 我们还可以删除要统计的元素,使用del关键字
    del c['jack']
    
    image.png
    1. 遍历counter中的所有元素,使用counter.elements()函数。
    值得注意的是:elements()函数会返回一个迭代器,这个迭代器中包含了所有的key,如果这个key对应的value小于1,则不会被elements()函数计算在内。
    1. 还可以调用counter.most_common([n])函数,获取数量最多的前n个元素。
      其中n是可选参数,如果n没有被指定,那么返回所有元素。

    2. 更牛逼的是,这个counter还支持集合操作。 +, -, &, |.

    from collections import Counter
    
    a = Counter('aaab')
    b = Counter('ccdd')
    
    # +
    a_sum_b = a + b
    print(a_sum_b)
    
    # -
    a_sub_b = a - b
    print(a_sub_b)
    
    # &, 交集
    a_and_b = a & b
    print(a_and_b)
    
    # |, 并集
    a_union_b = a | b
    print(a_union_b)
    
    image.png

    相关文章

      网友评论

          本文标题:collections.Counter的使用

          本文链接:https://www.haomeiwen.com/subject/hzivlctx.html