首頁  >  文章  >  後端開發  >  Python 計數器:如何使用 collections.Counter?

Python 計數器:如何使用 collections.Counter?

王林
王林轉載
2023-05-08 13:34:071017瀏覽

    一.介紹

    一個計數器工具提供快速且方便的計數,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 輸出結果

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

    1.3 用自帶的items()方法輸出

    顯然這個方法比轉換為字典後再輸出的方法更為方便:

    print(temp.items()) #dict_items([(&#39;e&#39;, 1), (&#39;c&#39;, 3), (&#39;b&#39;, 4), (&#39;d&#39;, 2), (&#39;a&#39;, 5)])
     
    for item in temp.items():
        print(item)
    """
    (&#39;a&#39;, 5)
    (&#39;c&#39;, 3)
    (&#39;d&#39;, 2)
    (&#39;e&#39;, 1)
    (&#39;b&#39;, 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(&#39;abracadabra&#39;).most_common(3)
    [(&#39;a&#39;, 5), (&#39;b&#39;, 2), (&#39;r&#39;, 2)]
    
    Counter(&#39;abracadabra&#39;).most_common(5)
    [(&#39;a&#39;, 5), (&#39;b&#39;, 2), (&#39;r&#39;, 2), (&#39;c&#39;, 1), (&#39;d&#39;, 1)]

    3. elements () 和sort()方法

    描述:傳回一個迭代器,其中每個元素將重複出現計數值指定次。元素會依照首次出現的順序傳回。如果一個元素的計數值小於1,elements() 將會忽略它。
    範例:

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

    官方文件範例:

    # Knuth&#39;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({&#39;a&#39;: 3, &#39;b&#39;: 0, &#39;c&#39;: -3, &#39;d&#39;: -6})
    
    #减去一个abcd
    str0 = Counter(&#39;aabbccdde&#39;)
    str0
    Counter({&#39;a&#39;: 2, &#39;b&#39;: 2, &#39;c&#39;: 2, &#39;d&#39;: 2, &#39;e&#39;: 1})
    
    str0.subtract(&#39;abcd&#39;)
    str0
    Counter({&#39;a&#39;: 1, &#39;b&#39;: 1, &#39;c&#39;: 1, &#39;d&#39;: 1, &#39;e&#39;: 1}
    subtract_test01 = Counter("AAB")
    subtract_test01.subtract("BCC")
    print(subtract_test01)  #Counter({&#39;A&#39;: 2, &#39;B&#39;: 0, &#39;C&#39;: -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中實作。

    • 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({&#39;a&#39;: 4, &#39;b&#39;: 3})
    c - d                       # subtract (keeping only positive counts)
    Counter({&#39;a&#39;: 2})
    c & d                       # intersection:  min(c[x], d[x]) 
    Counter({&#39;a&#39;: 1, &#39;b&#39;: 1})
    c | d                       # union:  max(c[x], d[x])
    Counter({&#39;a&#39;: 3, &#39;b&#39;: 2})
    print(Counter(&#39;AAB&#39;) + Counter(&#39;BCC&#39;))
    #Counter({&#39;B&#39;: 2, &#39;C&#39;: 2, &#39;A&#39;: 2})
    print(Counter("AAB")-Counter("BCC"))
    #Counter({&#39;A&#39;: 2})

    與」和「或」操作:

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

    單目加與減(一元運算子)意思是從空計數器加或減去,相當於給計數值乘以正值或負值,同樣輸出會忽略掉結果為零或小於零的計數:

    c = Counter(a=2, b=-4)
    +c
    Counter({&#39;a&#39;: 2})
    -c
    Counter({&#39;b&#39;: 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 = &#39;定位手机定位汽车定位GPS定位人定位位置查询&#39;         
    str_1 = &#39;导航定位手机定位汽车定位GPS定位人定位位置查询&#39;         
    
    str_sim(str_0,str_1,5)    
    0.75

    7. 計算元素總數、 Keys() 和Values()

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

    8. 查詢單元素結果

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

    9. 新增

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

    10. 刪除(del)

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

    11. 更新update()

    d = Counter("CCDD")
    c.update(d)
    print(c.most_common())  #[(&#39;B&#39;, 2), (&#39;A&#39;, 2), (&#39;C&#39;, 2), (&#39;D&#39;, 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(&#39;hello world&#39;)
    Counter({&#39;l&#39;: 3, &#39;o&#39;: 2, &#39;h&#39;: 1, &#39;e&#39;: 1, &#39; &#39;: 1, &#39;w&#39;: 1, &#39;r&#39;: 1, &#39;d&#39;: 1})
    # 统计单词数
    >>> collections.Counter(&#39;hello world hello world hello nihao&#39;.split())
    Counter({&#39;hello&#39;: 3, &#39;world&#39;: 2, &#39;nihao&#39;: 1})

    常用的方法:

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

    以上是Python 計數器:如何使用 collections.Counter?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

    陳述:
    本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除