Get indices of the top N values of a list

前端 未结 2 1349
迷失自我
迷失自我 2020-12-13 01:55

I have a list say a = [5,3,1,4,10]. I need to get indices of the top two values of the list, that is for 5 and 10 I would get [0

2条回答
  •  南方客
    南方客 (楼主)
    2020-12-13 02:01

    Just a NumPy alternative:

    import numpy as np
    
    top_2_idx = np.argsort(a)[-2:]
    top_2_values = [a[i] for i in top_2_idx]
    

提交回复
热议问题