深入了解Python中計(jì)數(shù)器Counter的使用
什么是容器Container
容器是容納對(duì)象的對(duì)象。它們提供了一種訪問(wèn)所包含對(duì)象并對(duì)其進(jìn)行迭代的方法。內(nèi)置容器的例子有元組、列表和字典。其他內(nèi)容包含在“collections”模塊中。
Counter是dict的子類。因此,它是一個(gè)無(wú)序的集合,其中元素和它們各自的計(jì)數(shù)被存儲(chǔ)為字典。這相當(dāng)于一個(gè)包或多個(gè)其他語(yǔ)言集。
語(yǔ)法
class collections.Counter([iterable-or-mapping])
初始化
計(jì)數(shù)器的構(gòu)造函數(shù)可以通過(guò)以下任一方式調(diào)用:
- 包含一系列項(xiàng)目
- 使用包含鍵和計(jì)數(shù)的字典
- 帶有將字符串名稱映射到計(jì)數(shù)的關(guān)鍵字參數(shù)
初始化計(jì)數(shù)器
# A Python program to show different ways to create # Counter from collections import Counter # With sequence of items print(Counter(['B','B','A','B','C','A','B','B','A','C'])) # with dictionary print(Counter({'A':3, 'B':5, 'C':2})) # with keyword arguments print(Counter(A=3, B=5, C=2))
輸出
Counter({'B': 5, 'A': 3, 'C': 2})
Counter({'B': 5, 'A': 3, 'C': 2})
Counter({'B': 5, 'A': 3, 'C': 2})
計(jì)數(shù)器更新
我們也可以通過(guò)以下方式創(chuàng)建一個(gè)空計(jì)數(shù)器:
coun = collections.Counter()
并且可以通過(guò)update() 方法進(jìn)行更新。相同的語(yǔ)法:
coun.update(Data)
# A Python program to demonstrate update() from collections import Counter coun = Counter() coun.update([1, 2, 3, 1, 2, 1, 1, 2]) print(coun) coun.update([1, 2, 4]) print(coun)
輸出
Counter({1: 4, 2: 3, 3: 1})
Counter({1: 5, 2: 4, 3: 1, 4: 1}
數(shù)據(jù)可以用初始化中提到的三種方式中的任何一種提供,計(jì)數(shù)器的數(shù)據(jù)將增加而不是替換。計(jì)數(shù)也可以為零或負(fù)數(shù)。
# Python program to demonstrate that counts in # Counter can be 0 and negative from collections import Counter c1 = Counter(A=4, B=3, C=10) c2 = Counter(A=10, B=3, C=4) c1.subtract(c2) print(c1)
輸出
Counter({'c': 6, 'B': 0, 'A': -6})
列表中的唯一計(jì)數(shù)
我們可以使用Counter來(lái)計(jì)算列表或其他集合中的不同元素。
# An example program where different list items are # counted using counter from collections import Counter # Create a list z = ['blue', 'red', 'blue', 'yellow', 'blue', 'red'] # Count distinct elements and print Counter aobject print(Counter(z))
輸出
Counter({'blue': 3, 'red': 2, 'yellow': 1})
打印計(jì)數(shù)器值
我們還可以使用keys() 、values() 和items() 方法訪問(wèn)計(jì)數(shù)器的所有鍵和值。這些方法分別返回計(jì)數(shù)器中的鍵、值和鍵值對(duì)的視圖。
from collections import Counter my_counter = Counter('abracadabra') print(my_counter.keys()) print(my_counter.values()) print(my_counter.items())
輸出
dict_keys(['a', 'b', 'r', 'c', 'd'])
dict_values([5, 2, 2, 1, 1])
dict_items([('a', 5), ('b', 2), ('r', 2), ('c', 1), ('d', 1)])
訪問(wèn)計(jì)數(shù)器
一旦初始化,計(jì)數(shù)器就像字典一樣被訪問(wèn)。此外,它不會(huì)引發(fā)KeyValue錯(cuò)誤(如果key不存在),而是值的計(jì)數(shù)顯示為0。
# Python program to demonstrate accessing of # Counter elements from collections import Counter # Create a list z = ['blue', 'red', 'blue', 'yellow', 'blue', 'red'] col_count = Counter(z) print(col_count) col = ['blue','red','yellow','green'] # Here green is not in col_count # so count of green will be zero for color in col: print (color, col_count[color])
輸出
Counter({'blue': 3, 'red': 2, 'yellow': 1})
blue 3
red 2
yellow 1
green 0
elements()
elements() 方法返回一個(gè)迭代器,該迭代器生成Counter已知的所有項(xiàng)。
注意:不包括count <= 0的元素。
# Python example to demonstrate elements() on # Counter (gives back list) from collections import Counter coun = Counter(a=1, b=2, c=3) print(coun) print(list(coun.elements()))
輸出
Counter({'c': 3, 'b': 2, 'a': 1})
['a', 'b', 'b', 'c', 'c', 'c']
most_common()
most_common() 用于產(chǎn)生N個(gè)最頻繁遇到的輸入值及其相應(yīng)計(jì)數(shù)的序列。
# Python example to demonstrate most_elements() on # Counter from collections import Counter coun = Counter(a=1, b=2, c=3, d=120, e=1, f=219) # This prints 3 most frequent characters for letter, count in coun.most_common(3): print('%s: %d' % (letter, count))
輸出
f: 219
d: 120
c: 3
到此這篇關(guān)于深入了解Python中計(jì)數(shù)器Counter的使用的文章就介紹到這了,更多相關(guān)python計(jì)數(shù)器counter內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python中構(gòu)建終端應(yīng)用界面利器Blessed模塊的使用
Blessed?庫(kù)作為一個(gè)輕量級(jí)且功能強(qiáng)大的解決方案,開(kāi)始在開(kāi)發(fā)者中贏得口碑,今天,我們就一起來(lái)探索一下它是如何讓終端UI開(kāi)發(fā)變得輕松而高效的吧2025-01-01自動(dòng)化測(cè)試Pytest單元測(cè)試框架的基本介紹
這篇文章主要介紹了Pytest單元測(cè)試框架的基本介紹,包含了Pytest的概念,Pytest特點(diǎn),其安裝流程步驟以及相關(guān)配置流程,有需要的朋友可以參考下2021-08-08Python繪圖Turtle庫(kù)的安裝問(wèn)題解決
這篇文章主要介紹了Python繪圖中解決Turtle的安裝問(wèn)題示例分析,也遇到過(guò)相同問(wèn)題的同學(xué)可以借鑒參考下,希望能夠解決你的問(wèn)題2021-10-10Python實(shí)現(xiàn)檢索指定網(wǎng)段內(nèi)所有的數(shù)據(jù)庫(kù)服務(wù)器
這篇文章主要為大家詳細(xì)介紹了如何使用Python實(shí)現(xiàn)檢索指定網(wǎng)段內(nèi)所有的數(shù)據(jù)庫(kù)服務(wù)器,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解下2025-02-02python opencv檢測(cè)目標(biāo)顏色的實(shí)例講解
下面小編就為大家分享一篇python opencv檢測(cè)目標(biāo)顏色的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-04-04tensorflow:指定gpu 限制使用量百分比,設(shè)置最小使用量的實(shí)現(xiàn)
今天小編就為大家分享一篇tensorflow:指定gpu 限制使用量百分比,設(shè)置最小使用量的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02Python?colorama?彩色打印實(shí)現(xiàn)代碼
這篇文章主要介紹了Python?colorama?彩色打印實(shí)現(xiàn)代碼,將介紹的類為Back,?它實(shí)現(xiàn)了與?Fore?類相同的九個(gè)關(guān)鍵字:BLACK、RED、GREEN、YELLOW、BLUE、MAGENTA、CYAN、WHITE、RESET,感興趣的朋友一起看看吧2022-04-04selenium學(xué)習(xí)教程之定位以及切換frame(iframe)
這篇文章主要給大家介紹了關(guān)于selenium學(xué)習(xí)教程之定位以及切換frame(iframe)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01Python中NumPy的線性代數(shù)子模塊linalg詳解
這篇文章主要介紹了Python中NumPy的線性代數(shù)子模塊linalg詳解,NumPy 的線性代數(shù)子模塊linalg提供了 20 余個(gè)函數(shù),用于求解行列式、逆矩陣、特征值、特征向量,以及矩陣分解等,需要的朋友可以參考下2023-08-08