[SciPy-user] one-liner to retrieve indices of n smallest values from an array?

Aaron Hoover ahoover at eecs.berkeley.edu
Wed Oct 18 00:09:14 EDT 2006


Hi Robert,

Thanks, is there a simple way to extend this to 2D arrays, or does  
that require the use of ravel()?

-Aaron
On Oct 17, 2006, at 8:52 PM, Robert Kern wrote:

> Aaron Hoover wrote:
>> Hi all,
>>
>> Perhaps this is better suited for the NumPy list, but is there a
>> simple (one line, perhaps) way to get the indices of the n smallest
>> elements of an array? Something along the lines of argmin() that
>> could take a parameter indicating
>> how many minimal values to return?
>
> a.argsort()[:n]
>
> -- 
> Robert Kern
>
> "I have come to believe that the whole world is an enigma, a  
> harmless enigma
>   that is made terrible by our own mad attempt to interpret it as  
> though it had
>   an underlying truth."
>    -- Umberto Eco
> _______________________________________________
> SciPy-user mailing list
> SciPy-user at scipy.org
> http://projects.scipy.org/mailman/listinfo/scipy-user




More information about the SciPy-User mailing list