Home > Net >  Sorting output from collections.Counter().most_common() in alphabetical order
Sorting output from collections.Counter().most_common() in alphabetical order

Time:04-13

collections.Counter().most_common() will return entries and their count values from most to least.

I assume for these entries with the same count values, these entries are returned in alphabetical order. However, I found that this is not the case. For example:

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)]

"leetcode" and "coding" are not returned in alphabetical order.

How can I sort from most common to least common, ordering by alphabetical order if two words' counts are the same?

CodePudding user response:

.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.

  • Related