I wanted to know the nth largest and smallest value in the list with python, It was a little different from what found article wanted, so I summarized it easily including the part that I thought it would be like this.
First, suppose you have the following list, similar to the original article.
a=[1,5,4,3,2]
If you want to get the index in ascending order of this list
>>> A=np.array(a)
>>> A.argsort()
array([0, 4, 3, 2, 1])
Will be.
Next, if you want to get the index in descending order (descending order)
>>> A=np.array(a)
>>> len(A)-1-A.argsort()
array([4, 0, 1, 2, 3])
In the original article, I implemented descending order using slices, but I tried it with the feeling that there is also this method because it is a little different from the specifications I want.
Recommended Posts