例如:[4,6,2,2,6,4,4,4]→[4,4,4,4,6,6,2,2]

一: 先统计每个元素出现次数,然后按照出现次数进行逆向排序。

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。
from collections import Counter
def frequency_count(list):
    counts = Counter(list)
    return sorted(list, key = lambda k : counts(k)*len(list)-list.index(k), reverse = True)

二:通过比较元组(-元素频率,元素index值)

def frequency_count(list)L
    return sorted(listed, key=lambda k:(-list.count(k),list.index(k)))

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄