When doing list operations, I often want an index up to the nth largest value. Until now, I kneaded it appropriately, but I knew that using numpy would make it smarter, so I made a note for myself.
a=[1,5,4,3,2]
When there was a list
>>> A=np.array(a)
>>> A.argsort()
array([0, 4, 3, 2, 1])
It is possible to get the index in ascending order by using argsort () for and ndarray. If you want to descend, use slices
>>> A.argsort()[::-1]
array([1, 2, 3, 4, 0])
Can be obtained
Recommended Posts