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.
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
nmost common elements and their counts from the most common to the least. Ifnis 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.