# 給定一個項目，我如何計算它在Python中的列表中的出現次數？

>>> l = list('aaaaabbbbcccdde')
>>> l
['a', 'a', 'a', 'a', 'a', 'b', 'b', 'b', 'b', 'c', 'c', 'c', 'd', 'd', 'e']

## list.count

list.count方法

>>> l.count('b')
4

>>> t = tuple('aabbbffffff')
>>> t
('a', 'a', 'b', 'b', 'b', 'f', 'f', 'f', 'f', 'f', 'f')
>>> t.count('f')
6

## collections.Counter

>>> from collections import Counter
>>> c = Counter(l)
>>> c['b']
4

### 進一步使用collections.Counter

>>> c.update(list('bbb'))
>>> c['b']
7
>>> c.subtract(list('bbb'))
>>> c['b']
4

>>> c2 = Counter(list('aabbxyz'))
>>> c - c2                   # set difference
Counter({'a': 3, 'c': 3, 'b': 2, 'd': 2, 'e': 1})
>>> c + c2                   # addition of all elements
Counter({'a': 7, 'b': 6, 'c': 3, 'd': 2, 'e': 1, 'y': 1, 'x': 1, 'z': 1})
>>> c | c2                   # set union
Counter({'a': 5, 'b': 4, 'c': 3, 'd': 2, 'e': 1, 'y': 1, 'x': 1, 'z': 1})
>>> c & c2                   # set intersection
Counter({'a': 2, 'b': 2})

## 為什麼不是熊貓？

list.count(x)返回x出現在列表中的次數

arr = np.array(['a','a','b','b','b','c'])
print(set(map(lambda x  : (x , list(arr).count(x)) , arr)))

{('c', 1), ('b', 3), ('a', 2)}

print(dict(map(lambda x  : (x , list(arr).count(x)) , arr)))

{'b': 3, 'c': 1, 'a': 2}

dict((i, a.count(i)) for i in a)

import numpy as np
a = np.array([1, 2, 3, 4, 1, 4, 1])
np.bincount(a)

>>> array([0, 3, 1, 1, 2])

>>> from collections import Counter
>>> z = ['blue', 'red', 'blue', 'yellow', 'blue', 'red']
>>> Counter(z)
Counter({'blue': 3, 'red': 2, 'yellow': 1})

dict((i,a.count(i)) for i in a)

def occurDict(items):
d = {}
for i in items:
if i in d:
d[i] = d[i]+1
else:
d[i] = 1
return d

### 計數一個項目

numpy.sum(numpy.array(a) == 1)

### 計數所有項目

numpy.bincount(a)

from collections import Counter
from collections import defaultdict
import numpy
import operator
import pandas
import perfplot

def counter(a):
return Counter(a)

def count(a):
return dict((i, a.count(i)) for i in set(a))

def bincount(a):
return numpy.bincount(a)

def pandas_value_counts(a):
return pandas.Series(a).value_counts()

def occur_dict(a):
d = {}
for i in a:
if i in d:
d[i] = d[i]+1
else:
d[i] = 1
return d

def count_unsorted_list_items(items):
counts = defaultdict(int)
for item in items:
counts[item] += 1
return dict(counts)

def operator_countof(a):
return dict((i, operator.countOf(a, i)) for i in set(a))

perfplot.show(
setup=lambda n: list(numpy.random.randint(0, 100, n)),
n_range=[2**k for k in range(20)],
kernels=[
counter, count, bincount, pandas_value_counts, occur_dict,
count_unsorted_list_items, operator_countof
],
equality_check=None,
logx=True,
logy=True,
)

2。

from collections import Counter
from collections import defaultdict
import numpy
import operator
import pandas
import perfplot

def counter(a):
return Counter(a)

def count(a):
return dict((i, a.count(i)) for i in set(a))

def bincount(a):
return numpy.bincount(a)

def pandas_value_counts(a):
return pandas.Series(a).value_counts()

def occur_dict(a):
d = {}
for i in a:
if i in d:
d[i] = d[i]+1
else:
d[i] = 1
return d

def count_unsorted_list_items(items):
counts = defaultdict(int)
for item in items:
counts[item] += 1
return dict(counts)

def operator_countof(a):
return dict((i, operator.countOf(a, i)) for i in set(a))

perfplot.show(
setup=lambda n: list(numpy.random.randint(0, 100, n)),
n_range=[2**k for k in range(20)],
kernels=[
counter, count, bincount, pandas_value_counts, occur_dict,
count_unsorted_list_items, operator_countof
],
equality_check=None,
logx=True,
logy=True,
)

li = ['A0','c5','A8','A2','A5','c2','A3','A9']

print sum(1 for el in li if el[0]=='A' and el[1] in '01234')

3 ，而不是6

>>> [1, 2, 3, 4, 1, 4, 1].count(1)
3

from collections import Counter
country=['Uruguay', 'Mexico', 'Uruguay', 'France', 'Mexico']
count_country = Counter(country)
output_list= []

for i in count_country:
output_list.append([i,count_country[i]])
print output_list

[['Mexico', 2], ['France', 1], ['Uruguay', 2]]

sum([1 for elem in <yourlist> if elem==<your_value>])