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物件中該元素個數
1 2 3 4 5
import collections.Counter
c = Counter(['eggs', 'ham']) c['bacon'] # 0 c['eggs'] # 1
刪除Counter物件中的元素記數
1 2
c['sausage'] = 0# counter entry with a zero count del c['sausage'] # del actually removes the entry