Name

argsort

Synopsis

argsort(a,axis=-1)

Returns a new integer array m with the same shape as a. Each vector of m along axis is the index sequence needed to sort the corresponding axis of a. In particular, if a has rank 1, the most common case, take( a ,argsort( a ))==sort( a ). For example:

x = [52, 115, 99, 111, 114, 101, 97, 110, 100, 55]
print Numeric.argsort(x)   # prints: [0 9 6 2 8 5 7 3 4 1]
print Numeric.sort(x)      
# prints: [52 55 97 99 100 101 110 111 114 115]
print Numeric.take(x, Numeric.argsort(x))
# prints: [52 55 97 99 100 101 110 111 114 115]

Here, the result of Numeric.argsort( x ) tells us that x’s smallest element is x [0], the second smallest is x [9], the third smallest is x [6], and so on. The call to Numeric.take in the last print statement takes x’s elements exactly in this order, and therefore produces the same sorted array as the call to Numeric.sort in the second print statement.

Get Python in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.