亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

深入了解Python中計(jì)數(shù)器Counter的使用

 更新時(shí)間:2023年09月04日 11:02:57   作者:python收藏家  
計(jì)數(shù)器counter是包含在collections模塊中的容器,這篇文章主要來(lái)和大家聊聊計(jì)數(shù)器counter的使用,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

什么是容器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)文章

最新評(píng)論