How to find the index of n largest elements in a list or np.array, Python

前端 未结 4 688
夕颜
夕颜 2021-02-05 09:05

Is there a built-in function or a very simple way of finding the index of n largest elements in a list or a numpy array?

K = [1,2,2,4,5,5,6,10]

4条回答
  •  执念已碎
    2021-02-05 10:01

    This should work:

    K = [1,2,2,4,5,5,6,10]
    num = 5
    print 'K %s.' % (sorted(K, reverse=True)[:num])
    

提交回复
热议问题