Back
You can use count method :
>>> from collections import Counter>>> a = ['violet', 'Indigo', 'violet', 'green', 'violet', 'indigo', 'violet' , ' green' , 'Indigo']>>> Counter(z)Counter({'violet': 4, 'Indigo': 2, 'green': 1})
>>> from collections import Counter>>> a = ['violet', 'Indigo', 'violet', 'green', 'violet', 'indigo', 'violet' , ' green' , 'Indigo']
>>> Counter(z)Counter({'violet': 4, 'Indigo': 2, 'green': 1})
Hope this helps, Cheers....!!
If you are working on Python 2.7 or 3 and you want the number of occurrences for each element then try this:
>>> from collections import Counter>>> z = ['blue', 'red', 'blue', 'yellow', 'blue', 'red']>>> Counter(z)Counter({'blue': 3, 'red': 2, 'yellow': 1})
>>> from collections import Counter
>>> z = ['blue', 'red', 'blue', 'yellow', 'blue', 'red']
>>> Counter(z)
Counter({'blue': 3, 'red': 2, 'yellow': 1})
If you only want one item's count, use the count method:
>>> [1, 2, 3, 4, 1, 4, 1].count(1) 3
>>> [1, 2, 3, 4, 1, 4, 1].count(1)
3
31k questions
32.8k answers
501 comments
693 users