我有一个要排序的数字列表,从1(最高)开始,然后打印出来。对于有联系的数字,我只想按顺序编号,以先到者为准。
lst = [[0.0] , [0.0] , [0.0] , [0.1] , [0.2]]
#expected output
rank_lst = [3,4,5,2,1]
Run Code Online (Sandbox Code Playgroud)
我只想要一个类似于我已经完成的简单功能:
rank_lst = [sorted(lst).index(values) for values in lst]
但是,此值从0开始,将最低的数字排序为0,然后对具有相同数字的绑定值进行排序,如下所示:
#output
rank_list = [0,0,0,1,2]
Run Code Online (Sandbox Code Playgroud)
cam*_*mil -1
>>> lst = [[0.0] , [0.0] , [0.0] , [0.1] , [0.2]]
>>> lst1 = [i[0] for i in lst] # you only need numbers, not lists for each value
>>> xx = list(enumerate(lst1)) # add an "index" to each value
>>> xx.sort(key=lambda i: -i[1]) # sort on values
>>> xxx = [(i[0], i[1], j+1) for j, i in enumerate(xx)] # add the rank of each value
>>> xxx.sort(key=lambda i : i[0]) # put them back in the original order using the index
>>> res = [i[2] for i in xxx] # get just the ranks
>>> res
[3, 4, 5, 2, 1]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
239 次 |
| 最近记录: |