[3, 3, 3, 4, 4, 2]
Would be:
[ (3, 3), (4, 2), (2, 1) ]
The output should be sorted by highest count first to low
def myfun(x,y):
return x[1]-y[1]
list1 = [3, 3, 3, 4, 4, 2]
s1 = set(list1)
newlist = []
for e in s1:
newlist.append((e,list1.count(e)))
print sorted(newlist,cmp=myfun)
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 python3