I have a counter like this :
counter = Counter(['a','a','b','b','b','c'])
which gives this object :
Counter({'b': 3, 'a': 2, 'c': 1})
and from that I want to creat a list such as :
list[0] = 'b'
list[1] = 'a'
list[2] = 'c'
any idea to do that the simpliest and fastest way possible please ? thanks
You can use collections.Counter.most_common
(which returns a list of the n most common elements and their counts from the most common to the least):
>>> counter.most_common()
[('b', 3), ('a', 2), ('c', 1)]
>>> [key for key, _ in counter.most_common()]
['b', 'a', 'c']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With