简体   繁体   English

按元素的频率排序python列表

[英]sorting a python list by frequency of elements

I have this code which sorts python list by frequency of elements. 我有这个代码按元素的频率对python列表进行排序。 It works for all other cases except when frequency of two elements are same. 它适用于所有其他情况,除非两个元素的频率相同。 If the frequency is same then I want to place smaller value first before higher value: 如果频率相同,那么我想在较高值之前先放置较小的值:

counts = collections.Counter(arr)
new_list = sorted(arr, key=lambda x: counts[x])

for item in new_list:
    print item

In case of [3,1,2,2,4] the output should be [1,3,4,2,2] but I get [3,1,4,2,2] . [3,1,2,2,4]的情况下,输出应该是[1,3,4,2,2]但我得到[3,1,4,2,2] How do I resolve this error? 我该如何解决这个错误?

你可以将你的密钥lambda函数设置为一个元组,因此,它将首先按counts[x]排序,如果有一个平局,它将按x排序,值本身。

 new_list = sorted(arr, key=lambda x: (counts[x], x))

You are only sorting by the number of item. 您只按项目数排序。 Under this logic, all the items which appear once have the same "weight", so python retains their original relevant position. 根据这个逻辑,出现一次的所有项目都具有相同的“权重”,因此python保留其原始相关位置。 Eg, 3 and 1 both appear once, so as far as their sorting is concerned, they are equivalent. 例如, 31都出现一次,因此就它们的排序而言,它们是等价的。 Since 3 comes before 1 in the original list, it is also placed first in the result. 由于3在原始列表中位于1之前,因此它也位于结果的第一位。

Your required output calls for a secondary sort criteria - the value of the element. 您所需的输出调用辅助排序条件 - 元素的值。 To do so, you must specify this explicitly: 为此,您必须明确指定:

new_list = sorted(arr, key=lambda x: (counts[x], x))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM