Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 按列表中出现的频率对列表进行排序_Python_List_Sorting - Fatal编程技术网

Python 按列表中出现的频率对列表进行排序

Python 按列表中出现的频率对列表进行排序,python,list,sorting,Python,List,Sorting,我有一个整数列表(甚至可以是字符串),我想按照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(),但它给出了元素的出现次数。 我想把它分类。知道怎么做吗 谢谢 或者: answer = [] for k in sorte

我有一个整数列表(甚至可以是字符串),我想按照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()
,但它给出了元素的出现次数。 我想把它分类。知道怎么做吗

谢谢

或者:

answer = []
for k in sorted(counts, key=counts.__getitem__, reverse=True):
    answer.extend([k for _ in range(counts[k])])
当然,
[k代表范围内的(计数[k])
可以替换为
[k]*计数[k]

所以第17行变成了

list(itertools.chain.from_iterable([[k]*counts[k] for k in sorted(counts, key=counts.__getitem__, reverse=True)]))
甚至更好(高效)的实施

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]

如果你喜欢就地分拣

a.sort(key=Counter(a).get, reverse=True)

使用Python 3.3和内置函数,以计数为键:

>>> 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]
不是很有趣的方式

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), [])

如果您碰巧已经在使用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]

在数组中以及在相同大小的集合中出现:

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
结果:
list.count
我相信这会让它效率很低。@thefourtheye我不得不确定它的时间,但听起来不错。诚然,对于像示例这样的小列表来说,这是非常安全的。输出中
4
3
的顺序是否重要?不,这其实并不重要,如果它使它更简单,否则我必须重新排序:-)@aश威尼चhaudhary:我考虑过这一点,但如果元素不是原始类型,它可能不会特别好地工作。引用等等……如果您担心可变类型,那么
计数器根本就不起作用。
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])
In [314]: np.repeat(vals[order], counts[order]).tolist()
Out[314]: [5, 5, 5, 5, 4, 4, 4, 3, 3, 3, 1, 1, 2]
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]