[Numpy-discussion] indexes in an array where value is greater than 1?

Benjamin Root ben.root at ou.edu
Fri May 25 11:21:08 EDT 2012


On Fri, May 25, 2012 at 11:17 AM, Chris Withers <chris at simplistix.co.uk>wrote:

> Hi All,
>
> I have an array:
>
> arrrgh = numpy.zeros(100000000)
>
> A sparse collection of elements will have values greater than zero:
>
> arrrgh[9999] = 2
> arrrgh[3453453] =42
>
> The *wrong* way to do this is:
>
> for i in xrange(len(arrrgh)):
>     if arrrgh[i] > 1:
>         print i
>
> What's the right way?
>
> Chris
>
>
np.nonzero(arrrgh > 1)

Note, it returns a list of lists, one for each dimension of the input array.

Cheers!
Ben Root
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20120525/1b843461/attachment.html>


More information about the NumPy-Discussion mailing list