Python中Collections.counter用法
Counter objects
A counter tool is provided to support convenient and rapid tallies. For example:
>>> # Tally occurrences of words in a list
>>> cnt = Counter()
>>> for word in ['red', 'blue', 'red', 'green', 'blue', 'blue']:
... cnt[word] += 1
>>> cnt
Counter({'blue': 3, 'red': 2, 'green': 1})
>>> # Find the ten most common words in Hamlet
>>> import re
>>> words = re.findall(r'\w+', open('hamlet.txt').read().lower())
>>> Counter(words).most_common(10)
[('the', 1143), ('and', 966), ('to', 762), ('of', 669), ('i', 631),
('you', 554), ('a', 546), ('my', 514), ('hamlet', 471), ('in', 451)]
class collections.
Counter
([iterable-or-mapping])
A Counter
is a dict
subclass for counting hashable objects. It is an unordered collection where elements are stored as dictionary keys and their counts are stored as dictionary values. Counts are allowed to be any integer value including zero or negative counts. The Counter
class is similar to bags or multisets in other languages.
計數器是一個用於計算可雜湊物件的dict子類,它是一個無序的集合,其中的元素儲存為字典鍵,它們的計數儲存為字典值。它是一個無序的集合,其中元素被儲存為字典鍵,而它們的計數被儲存為字典值。計數可以是任何整數值,包括零或負數。Counter類類似於其他語言中的bag或multisets。
Elements are counted from an iterable or initialized from another mapping (or counter):元素的計數來自一個可迭代的或從另一個對映(或計數器)初始化的。
>>> c = Counter() # a new, empty counter
>>> c = Counter('gallahad') # a new counter from an iterable
>>> c = Counter({'red': 4, 'blue': 2}) # a new counter from a mapping
>>> c = Counter(cats=4, dogs=8) # a new counter from keyword args
Counter objects have a dictionary interface except that they return a zero count for missing items instead of raising a KeyError
:計數器物件有一個字典介面,除了它們對丟失的專案返回一個零計數,而不是引發一個KeyError。
>>> c = Counter(['eggs', 'ham'])
>>> c['bacon'] # count of a missing element is zero
0
Setting a count to zero does not remove an element from a counter. Use del
to remove it entirely:將計數設定為零並不會從計數器中刪除一個元素。使用del可以完全刪除它。
>>> c['sausage'] = 0 # counter entry with a zero count
>>> del c['sausage'] # del actually removes the entry
相關文章
- Python中的collections.Counter模組Python
- python中collections.Counter是什麼?Python
- 一道演算法題,引出collections.Counter的特殊用法演算法
- python中return的用法Python
- python中的eval用法Python
- Python中set的用法Python
- python 中 pipenv 用法筆記Python筆記
- Python中return self的用法Python
- Python 中 Requests 庫的用法Python
- Python 中filter函式用法PythonFilter函式
- python中zip()函式的用法Python函式
- 淺談python中的xpath用法Python
- Python 中求和函式 sum 用法Python函式
- python 中chr(),unichr(),ord()的用法Python
- Python中paramiko 模組的用法Python
- [PY3]——找出一個序列中出現次數最多的元素/collections.Counter 類的用法
- PyThon range()函式中for迴圈用法Python函式
- Python中__init__的用法和理解Python
- Python中的split()函式的用法Python函式
- python 中 print 函式用法總結Python函式
- Python 中 Unicode 的正確用法PythonUnicode
- Python 中的map、reduce函式用法Python函式
- Python中print用法及The difference between Python 2 and 3Python
- Python numpy中矩陣的用法總結Python矩陣
- Python中的selenium的簡單用法Python
- Python中insert用法及實戰案例!Python
- Python中典型內建函式的用法Python函式
- Python中enumerate函式用法詳解Python函式
- python中sorted()和list.sort()的用法Python
- Python中迴圈語句中的else用法Python
- 由 sort 中 key 的用法淺談 pythonPython
- python with 用法Python
- Python3中strip()、lstrip()、rstrip()用法詳解Python
- python中upper函式的用法是什麼?Python函式
- Python 中 key 引數的含義及用法Python
- 詳解Python中yield生成器的用法Python
- python print 用法Python
- python xpath用法Python