@SilentGhost has a great solution for Python 2.7+. A relatively simple solution for version 2.6 and later:
a = [8, 8, 1, 1, 5, 8, 9] popularity = sorted(set(a), key=lambda x: -a.count(x)) [8, 1, 5, 9]
This solution, however, is expensive (due to count ).
Here's another, better solution with a temporary dictionary:
a = [8, 8, 1, 1, 5, 8, 9] d = {} for i in a: d[i] = d.get(i, 0) + 1 popularity = sorted(d, key=d.get, reverse=True)
eumiro
source share