Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

collections.Counter().most_common() not sorted in alphabetical order

collections.Counter().most_common() will return entries and their count values from most to least.
I assume for these entries with same count values, these entries are returned in alphabetical order. However, I found it is not the case. For exapmle:

a =  ["i","i","love","love","leetcode","coding"]

b = Counter(a)

print(b.most_common())

this is what I got:

[('i', 2), ('love', 2), ('leetcode', 1), ('coding', 1)]

see this "leetcode" and "coding" string are not returned in alphabetical order.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

How can I make it sorted from most common to least common and in alphabetical order if counts are the same

>Solution :

.most_common() will order the elements in decreasing order of frequency, with tiebreaks based on when an element first appears in the list.

The documentation for .most_common() states (emphasis mine):

Return a list of the n most common elements and their counts from the most common to the least. If n is omitted or None, most_common() returns all elements in the counter. Elements with equal counts are ordered in the order first encountered.

So, you should use:

sorted(b.most_common(), key=lambda x: (x[1], x[0]))

to resort the elements within each count alphabetically, explicitly encoding the lexicographical order of the element as a tiebreaker.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading