Python - AI 助手

Python collections.Counter



Python Counter 是一個容器,用於儲存物件的計數。它用於統計迭代物件中存在的專案數量。計數可以是任何整數值,包括零或負數。

Counter 是字典的子類。它以鍵值對的形式表示資料。它繼承了字典的所有方法和屬性。它允許執行算術和集合運算。它可以與任何實現迭代協議的迭代物件一起使用。

語法

以下是 Python Counter 的語法:

class collections.Counter([iterable-or-mapping])

引數

此資料型別接受迭代物件作為引數。

返回值

此資料型別返回計數器物件。

Counter 的初始化

Counter 使用迭代物件作為輸入值進行初始化。以下是初始化 Counter 的幾種不同方法:

  • 使用一系列專案
  • 使用包含鍵和計數的字典
  • 使用將字串名稱對映到計數的關鍵字引數

示例

在下面的示例中,我們以不同的方式初始化了 Counter

from collections import Counter
# With sequence of items 
print(Counter(['x','x','z','x','y','z','x','x','z','x']))
# with dictionary
print(Counter({'y':3, 'z':5, 'x':2}))
# with keyword arguments
print(Counter(z=3, x=5, y=2))

以下是上述程式碼的輸出:

Counter({'x': 6, 'z': 3, 'y': 1})
Counter({'z': 5, 'y': 3, 'x': 2})
Counter({'x': 5, 'z': 3, 'y': 2})

示例

以下是 Python 中 Counter 的基本示例:

from collections import Counter
# Create a list
tuple1 = ('Python', 'Java', 'Python', 'C++', 'Python', 'Java')
# Count distinct elements and print Counter a object
print(Counter(tuple1))

以下是上述程式碼的輸出:

Counter({'Python': 3, 'Java': 2, 'C++': 1})

Counter 值

我們還可以使用 keys()values()items() 方法訪問計數器的所有鍵、值和專案。

示例

在這裡,我們定義了 Counter 並找到了它的鍵值、值和專案:

from collections import Counter
#defined Counter
my_counter = Counter('xyzmnoxyzm')
#Finding key values
print(my_counter.keys())
#Finding values 
print(my_counter.values())
#Finding items 
print(my_counter.items()) 

以下是上述程式碼的輸出:

dict_keys(['x', 'y', 'z', 'm', 'n', 'o'])
dict_values([2, 2, 2, 2, 1, 1])
dict_items([('x', 2), ('y', 2), ('z', 2), ('m', 2), ('n', 1), ('o', 1)])

Counter 中的方法

以下是 Counter() 類中定義的不同方法:

方法 功能
update() 用於將元素或迭代物件新增到現有的 Counter 中
total() 計算計數的總和
most_common() 返回 n 個最常見元素及其計數的列表,從最常見到最不常見。如果省略 n 或為 None,most_common() 將返回計數器中的所有元素
elements() 返回一個迭代器,該迭代器重複每個元素與其計數一樣多的次數。元素按照首次遇到的順序返回。
subtract() 從迭代物件或另一個對映(或計數器)中減去元素。

Python Counter.update() 方法

Counter 類中的 update() 方法用於向計數器新增新元素。

示例

在這裡,我們建立了一個空計數器,並使用 update() 函式添加了元素,Counter 返回具有相應計數的元素:

from collections import Counter
#empty counter
var1 = Counter()
#updated with elements
var1.update([2,4,6,2,4,6,2,6])
print(var1)
var1.update([2, 6, 4])
print(var1)

以下是上述程式碼的輸出:

Counter({2: 3, 6: 3, 4: 2})
Counter({2: 4, 6: 4, 4: 3})

Python Counter.subtract() 方法

Counter 類中的 subtract() 方法用於在兩個計數器之間執行減法運算。兩個計數器的減法結果可以是 負值

示例

在這裡,我們定義了兩個計數器,c1c2,並將 c2 從 c1 中減去:

from collections import Counter
#defined counters
c1 = Counter(A=4, B=3, C=10)
c2 = Counter(A=10, B=3, C=4)
#subtraction of c2 from cl
c1.subtract(c2)
print(c1)

以下是上述程式碼的輸出:

Counter({'C': 6, 'B': 0, 'A': -6})

Python Counter.total() 方法

Counter 類中的 total() 方法用於計算計數器中所有元素計數的總和。

示例

在這裡,我們定義了一個列表並將其轉換為 Counter,然後找到所有元素計數的總和:

from collections import Counter
list1 = ['a', 'b', 'c', 'a', 'b', 'c', 'a']
count = Counter(list1).total()
print("Total elements of the list :", count)

以下是上述程式碼的輸出:

Total elements of the list : 7

Python Counter.most_common() 方法

Python 的 Counter 類中的 `most_common()` 方法用於返回計數器中最常見的 n 個元素的列表。它按元素計數從高到低的順序返回元素,從最常見到最不常見。如果未指定 n 值,則將返回計數器中的所有元素。

示例

以下是 Counter 的 `most_common()` 方法示例:

from collections import Counter
tup1 = (2,56,13,4,2,10,13,2, )
count = Counter(tup1).most_common(2)
print("Most common elements of the tuple :", count)

以下是上述程式碼的輸出:

Most common elements of the tuple : [(2, 3), (13, 2)]
python_modules.htm
廣告