文章目录
简介
计数器是用于计算可哈希对象的字典子类。 它是一个集合,其中元素存储为字典键 它们的计数存储为字典值。允许计数 任何整数值,包括零计数或负计数。计数器类类似于其他语言中的袋子或多集。
元素从可迭代对象计数或从另一个映射(或计数器)初始化:
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