In python, how do I take the highest occurrence of something in a list, and sort it that way? -
[3, 3, 3, 4, 4, 2]
would be:
[ (3, 3), (4, 2), (2, 1) ]
the output should sorted highest count first lowest count. in case, 3 2 1.
data = [3, 3, 3, 4, 4, 2] result = [] entry in set(data): result.append((entry, data.count(entry))) result.sort(key = lambda x: -x[1]) print result >>[(3, 3), (4, 2), (2, 1)]
Comments
Post a Comment