I was trying to sort few values in list using Python's Counter from collection module. But it gives weird result when
>>> diff=["aaa","aa","a"]
>>> c=Counter(diff)
>>> sorted(c.items(), key = lambda x:x[1] , reverse=True)
[('aa', 1), ('a', 1), ('aaa', 1)]
>>> c.items()
[('aa', 1), ('a', 1), ('aaa', 1)]
Output is strange, as it seems to have shuffle 'aa' to the first place, then 'a' and 'aaa' at last. Ideally, it should have been 'a' then 'aa' then 'aaa'
What is the reason behind this and how would you rectify the same
Edit: Most people understand the question incorrectly, Hence I am pushing some clarifications. The goal is to sort the number of words in list based on it's occurances.
Let's say list diff = ["this", "this", "world", "cool", "is", "cool", "cool"]
. The final output by my above code would be cool
then this
then is
then world
which is correct.
but problem is when you supply same characters with same occurences, python misbehaves. As the Input is diff = ["aaa", "aa", "a"]
, I expected output to be a
then aa
then aaa
. But python algorithm would never know as every word occurred single time.
But if that is the case, then why did python didn't printed aaa
then aa
then a
(i.e in same order it was inputted) giving benefit of doubt. Python sort did actually swapped . WHY?