myList = [1, 2, 3, 4, 1, 4, 1]
print myList.count(1)
3
from collections import Counter
myList = [1, 2, 3, 4, 1, 4, 1]
print "---Counter()---"
result = Counter(myList)
print result
for key in result:
print key, result[key]
#If you want to find out the value you counted without a key,
print "---Counter().values()---"
result = Counter(myList).values()
print result
Results are
---Counter()---
Counter({1: 3, 4: 2, 2: 1, 3: 1})
1 3
2 1
3 1
4 2
---Counter().values()---
[3, 1, 1, 2]t
© 2024 OneMinuteCode. All rights reserved.