[3, 3, 3, 4, 4, 2]
Would be:
[ (3, 3), (4, 2), (2, 1) ]
The output should be sorted by highest count first to lowest count. In this case, 3 to 2 to 1.
[3, 3, 3, 4, 4, 2]
Would be:
[ (3, 3), (4, 2), (2, 1) ]
The output should be sorted by highest count first to lowest count. In this case, 3 to 2 to 1.
Try using a collections.Counter:
I think, this is what you asked for. Sorry for hurry with the first answer. But just note that
cmp
argument for sorted is not available in python3Why would you choose an O(n**2) algorithm to do this. The alternative to Counter (if you have <2.7) is not too difficult
You can use a Counter in Python 2.7+ (this recipe works on 2.5+):