Let’s start from ‘Counter’(https://docs.python.org/2/library/collections.html).
- built-in container: ‘dict’, ‘list’, ‘set’ ‘tuple’
1
2
3
4
5
6
7
8
9
10
11from collections import Counter
test_src = ['a','b','a','b','c','b','a']
result = Counter(test_src)
elements = list(result.elements())
most_common = result.most_common()
print(result)
print(elements)
print(most_common)Counter({‘a’: 3, ‘b’: 3, ‘c’: 1})
[‘a’, ‘a’, ‘a’, ‘b’, ‘b’, ‘b’, ‘c’]
[(‘a’, 3), (‘b’, 3), (‘c’, 1)]