How to find the index of second largest element in an array collection?

后端 未结 3 652
攒了一身酷
攒了一身酷 2021-01-25 08:43

I have a function which calculates sum of money spent on each checkout on a counter, After calculation, i want to find the best two checkout counter, I am able to find the index

3条回答
  •  一个人的身影
    2021-01-25 09:15

    Don't make it complicated, it is simple:

    INDX = (-ARRAY).argsort()[:N]
    

    where N is the number of elements you are searching for, e.g. N=2 (two largest numbers) ARRAY is your numpy array which you are searching in, e.g. ARRAY=np.array[10.1,9,10,5,1,3] and, INDX is a list of the indices of the elements within the ARRAY. Obviously the length of INDX is equal to N.

提交回复
热议问题