2010-07-03 31 views

cevap

31

Python2.7 +

>>> from collections import Counter 
>>> L=['a','b','a','b'] 
>>> print(Counter(L)) 
Counter({'a': 2, 'b': 2}) 
>>> print(Counter(L).items()) 
dict_items([('a', 2), ('b', 2)]) 

python2.5/2,6

>>> from collections import defaultdict 
>>> L=['a','b','a','b'] 
>>> d=defaultdict(int) 
>>> for item in L: 
>>>  d[item]+=1 
>>>  
>>> print d 
defaultdict(<type 'int'>, {'a': 2, 'b': 2}) 
>>> print d.items() 
[('a', 2), ('b', 2)] 
+0

Python 2.5 herhangi bir çözüm? Bunu Google App Engine – demos

+1

ile kullanıyorum Elbette, varsayılanı kullanabilirsiniz. Yanıtıma ekleyeceğim –

+1

Sayacın 2,5 sürümü için http://code.activestate.com/recipes/576611/ adresine bakın. – sdolan

İlgili konular