深入了解Python中計數(shù)器Counter的使用
什么是容器Container
容器是容納對象的對象。它們提供了一種訪問所包含對象并對其進行迭代的方法。內置容器的例子有元組、列表和字典。其他內容包含在“collections”模塊中。
Counter是dict的子類。因此,它是一個無序的集合,其中元素和它們各自的計數(shù)被存儲為字典。這相當于一個包或多個其他語言集。
語法
class collections.Counter([iterable-or-mapping])
初始化
計數(shù)器的構造函數(shù)可以通過以下任一方式調用:
- 包含一系列項目
- 使用包含鍵和計數(shù)的字典
- 帶有將字符串名稱映射到計數(shù)的關鍵字參數(shù)
初始化計數(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})
計數(shù)器更新
我們也可以通過以下方式創(chuàng)建一個空計數(shù)器:
coun = collections.Counter()
并且可以通過update() 方法進行更新。相同的語法:
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ù)可以用初始化中提到的三種方式中的任何一種提供,計數(shù)器的數(shù)據(jù)將增加而不是替換。計數(shù)也可以為零或負數(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})
列表中的唯一計數(shù)
我們可以使用Counter來計算列表或其他集合中的不同元素。
# 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})
打印計數(shù)器值
我們還可以使用keys() 、values() 和items() 方法訪問計數(shù)器的所有鍵和值。這些方法分別返回計數(shù)器中的鍵、值和鍵值對的視圖。
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)])
訪問計數(shù)器
一旦初始化,計數(shù)器就像字典一樣被訪問。此外,它不會引發(fā)KeyValue錯誤(如果key不存在),而是值的計數(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() 方法返回一個迭代器,該迭代器生成Counter已知的所有項。
注意:不包括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() 用于產生N個最頻繁遇到的輸入值及其相應計數(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
到此這篇關于深入了解Python中計數(shù)器Counter的使用的文章就介紹到這了,更多相關python計數(shù)器counter內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python實現(xiàn)檢索指定網段內所有的數(shù)據(jù)庫服務器
這篇文章主要為大家詳細介紹了如何使用Python實現(xiàn)檢索指定網段內所有的數(shù)據(jù)庫服務器,文中的示例代碼講解詳細,感興趣的小伙伴可以了解下2025-02-02tensorflow:指定gpu 限制使用量百分比,設置最小使用量的實現(xiàn)
今天小編就為大家分享一篇tensorflow:指定gpu 限制使用量百分比,設置最小使用量的實現(xiàn),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02Python?colorama?彩色打印實現(xiàn)代碼
這篇文章主要介紹了Python?colorama?彩色打印實現(xiàn)代碼,將介紹的類為Back,?它實現(xiàn)了與?Fore?類相同的九個關鍵字:BLACK、RED、GREEN、YELLOW、BLUE、MAGENTA、CYAN、WHITE、RESET,感興趣的朋友一起看看吧2022-04-04selenium學習教程之定位以及切換frame(iframe)
這篇文章主要給大家介紹了關于selenium學習教程之定位以及切換frame(iframe)的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-01-01Python中NumPy的線性代數(shù)子模塊linalg詳解
這篇文章主要介紹了Python中NumPy的線性代數(shù)子模塊linalg詳解,NumPy 的線性代數(shù)子模塊linalg提供了 20 余個函數(shù),用于求解行列式、逆矩陣、特征值、特征向量,以及矩陣分解等,需要的朋友可以參考下2023-08-08