更新时间:2022-05-04 23:30:17
仍然适用于NumPy数组的非NumPy解决方案是使用 OrderedCounter
,后跟 sorted
和自定义功能:
A non-NumPy solution, which does still work with NumPy arrays, is to use an OrderedCounter
followed by sorted
with a custom function:
from collections import OrderedDict, Counter
class OrderedCounter(Counter, OrderedDict):
pass
L = [3,4,5,1,2,4,1,1,2,4]
c = OrderedCounter(L)
keys = list(c)
res = sorted(c, key=lambda x: (-c[x], keys.index(x)))
print(res)
[4, 1, 2, 3, 5]