我有一个整数列表(或者甚至可以是字符串),我想根据Python中出现的频率对其进行排序,例如:
a = [1, 1, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 5]
此处元素5
在列表中出现4次,4
出现3次。因此输出排序列表将是:
result = [5, 5, 5, 5, 3, 3, 3, 4, 4, 4, 1, 1, 2]
我尝试使用a.count()
,但它给出了元素的出现次数。
我想对它进行排序。知道怎么做吗?
由于
答案 0 :(得分:24)
from collections import Counter
print [item for items, c in Counter(a).most_common() for item in [items] * c]
# [5, 5, 5, 5, 3, 3, 3, 4, 4, 4, 1, 1, 2]
甚至更好(有效)的实施
from collections import Counter
from itertools import repeat, chain
print list(chain.from_iterable(repeat(i, c) for i,c in Counter(a).most_common()))
# [5, 5, 5, 5, 3, 3, 3, 4, 4, 4, 1, 1, 2]
或者
from collections import Counter
print sorted(a, key=Counter(a).get, reverse=True)
# [5, 5, 5, 5, 3, 3, 3, 4, 4, 4, 1, 1, 2]
如果您更喜欢就地排序
a.sort(key=Counter(a).get, reverse=True)
答案 1 :(得分:3)
In [15]: a = [1,1,2,3,3,3,4,4,4,5,5,5,5]
In [16]: counts = collections.Counter(a)
In [17]: list(itertools.chain.from_iterable([[k for _ in range(counts[k])] for k in sorted(counts, key=counts.__getitem__, reverse=True)]))
Out[17]: [5, 5, 5, 5, 3, 3, 3, 4, 4, 4, 1, 1, 2]
可替换地:
answer = []
for k in sorted(counts, key=counts.__getitem__, reverse=True):
answer.extend([k for _ in range(counts[k])])
当然,[k for _ in range(counts[k])]
可以替换为[k]*counts[k]
因此第17行变为
list(itertools.chain.from_iterable([[k]*counts[k] for k in sorted(counts, key=counts.__getitem__, reverse=True)]))
答案 2 :(得分:3)
使用Python 3.3和内置的sorted函数,以count作为键:
>>> a = [1,1,2,3,3,3,4,4,4,5,5,5,5]
>>> sorted(a,key=a.count)
[2, 1, 1, 3, 3, 3, 4, 4, 4, 5, 5, 5, 5]
>>> sorted(a,key=a.count,reverse=True)
[5, 5, 5, 5, 3, 3, 3, 4, 4, 4, 1, 1, 2]
答案 3 :(得分:0)
没有趣的方式......
a = [1,1,2,3,3,3,4,4,4,5,5,5,5]
from collections import Counter
result = []
for v, times in sorted(Counter(a).iteritems(), key=lambda x: x[1], reverse=True):
result += [v] * times
一个班轮:
reduce(lambda a, b: a + [b[0]] * b[1], sorted(Counter(a).iteritems(), key=lambda x: x[1], reverse=True), [])
答案 4 :(得分:0)
如果您碰巧已经在使用numpy,或者如果可以使用numpy,那么这里是另一种选择:
In [309]: import numpy as np
In [310]: a = [1, 2, 3, 3, 1, 3, 5, 4, 4, 4, 5, 5, 5]
In [311]: vals, counts = np.unique(a, return_counts=True)
In [312]: order = np.argsort(counts)[::-1]
In [313]: np.repeat(vals[order], counts[order])
Out[313]: array([5, 5, 5, 5, 4, 4, 4, 3, 3, 3, 1, 1, 2])
结果是一个numpy数组。如果要结束于Python列表,请调用数组的tolist()
方法:
In [314]: np.repeat(vals[order], counts[order]).tolist()
Out[314]: [5, 5, 5, 5, 4, 4, 4, 3, 3, 3, 1, 1, 2]
答案 5 :(得分:0)
在数组中以及在大小相等的集合中出现:
rev=True
arr = [6, 6, 5, 2, 9, 2, 5, 9, 2, 5, 6, 5, 4, 6, 9, 1, 2, 3, 4, 7 ,8 ,8, 8, 2]
print arr
arr.sort(reverse=rev)
ARR = {}
for n in arr:
if n not in ARR:
ARR[n] = 0
ARR[n] += 1
arr=[]
for k,v in sorted(ARR.iteritems(), key=lambda (k,v): (v,k), reverse=rev):
arr.extend([k]*v)
print arr
[6, 6, 5, 2, 9, 2, 5, 9, 2, 5, 6, 5, 4, 6, 9, 1, 2, 3, 4, 7, 8, 8, 8, 2]
[2, 2, 2, 2, 2, 6, 6, 6, 6, 5, 5, 5, 5, 9, 9, 9, 8, 8, 8, 4, 4, 7, 3, 1]