python 数字列表排序,输出对应的索引 | 转载

参考自: https://blog.csdn.net/qq_33757398/article/details/108355003

例如输入列表: [8, 2, 4]
期望输出: [2, 3, 1]
一句话可以搞定:rank = [index+1 for index, value in sorted(list(enumerate(input_list)), key=lambda x:x[1])]

案例

L = [0.6, 0.3, 1.5, 0, 2.1, 0.2]

res = [idx+1 for idx,val in sorted(list(enumerate(L)), key = lambda x:x[1], reverse = True)]
print(res)

效果

原文地址:https://www.cnblogs.com/Higgerw/p/14238576.html