1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
from collections import Counter, OrderedDict
counter = Counter(["a", "a", "b", "b", "b"])
counter
# Counter({'b': 3, 'a': 2})
sorted_by_freq_tuples = sorted(counter.items(), key=lambda x: x[1], reverse=True)
sorted_by_freq_tuples
# [('b', 3), ('a', 2)]
ordered_dict = OrderedDict(sorted_by_freq_tuples)
# OrderedDict([('b', 3), ('a', 2)])
v1 = vocab(ordered_dict)
# Vocab()
|