2015-08-16 37 views

回答

0

使用collections.Counter

>>> from collections import counter 
>>> a=['apple' , 'lemon' , 'apple' , 'apple' , 'lemon'] 
>>> Counter(a) 
Counter({'apple': 3, 'lemon': 2}) 

計數器的字典是用於計算哈希的對象的字典子類。它是一個無序的集合,其元素以字典鍵的形式存儲,並將其計數存儲爲字典值。計數允許爲包括零或負計數的任何整數值。 Counter類與其他語言的bag或multisets類似。