Intro

两种方法统计list中的元素出现次数。

Example 1

1
2
3
4
5
6
7
8
9
10
 >>> a = [1,1,1,1,2,2,2,2,3,3,4,5,5]
 >>> d = {x:a.count(x) for x in a}
 >>> d
 {1: 4, 2: 4, 3: 2, 4: 1, 5: 2}
 >>> a, b = d.keys(), d.values()
 >>> a
 dict_keys([1, 2, 3, 4, 5])
 >>> b
 dict_values([4, 4, 2, 1, 2])
 

Example 2(In Python 2.7+)

>>> import collections
>>> a = [1,1,1,1,2,2,2,2,3,3,4,5,5]
>>> counter=collections.Counter(a)
>>> d = collections.Counter(a)
>>> d
Counter({1: 4, 2: 4, 3: 2, 5: 2, 4: 1})
>>> a, b = d.keys(), d.values()
>>> a
dict_keys([1, 2, 3, 4, 5])
>>> b
dict_values([4, 4, 2, 1, 2])

reference

stackoverflow