[Numpy-discussion] Getting indices from numpy array with condition

Francesc Alted faltet at pytables.org
Tue Nov 18 05:39:53 EST 2008


A Tuesday 18 November 2008, Nicolas ROUX escrigué:
> Hi,
>
> Maybe this is not so clever, but I can't find it in the doc.
> I need to get all indices/index of all occurrences of a value in a
> numpy array
>
>
> As example:
>
> a = numpy.array([1,2,3],[4,5,6],[7,8,9])
> I need to get the indice/index of all array elements where a[a>3]
>
> Any fast/easy way to write this ?

Perhaps the nonzero() method is what you are looking for:

In [9]: (a > 3).nonzero()
Out[9]: (array([1, 1, 1, 2, 2, 2]), array([0, 1, 2, 0, 1, 2]))

Cheers,

-- 
Francesc Alted



More information about the NumPy-Discussion mailing list