[Numpy-discussion] using the functions nonzero and where

Dennis Cooke dennis.a.cooke at gmail.com
Sat Apr 21 00:50:29 EDT 2007


I'm an ex-Matlab user trying to come up to speed with python and numpy.  I'm
confused on how to use the Numpy functions nonzero() and where().  In
matlab, the equivalent function (find(a>0) ) would return an array, whereas
in numpy, where() or nonzero() will return a single element tuple.  For
example:

In [35]: x = [ 0, 0, 0, 99, 0, 1, 5]
# and I wish to file the index of the first non-zero element of x - which is
3.

In [36]: from numpy import nonzero
In [37]: i=nonzero(x)
In [38]: i
Out[38]: (array([3, 5, 6]),)
In [39]: i[0]
Out[39]: array([3, 5, 6])

so nonzero() has output a tuple with a single element = the string
'array([3, 5, 6])'
How do I convert this string (or the original tuple)  into an array where
the first element = 3 (the index of the first non-zero element of my
original array x).
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20070421/f98998ab/attachment.html>


More information about the NumPy-Discussion mailing list