[Numpy-discussion] using the functions nonzero and where

Robert Kern robert.kern@gmail....
Tue Apr 24 12:34:25 CDT 2007


Christopher Barker wrote:
> Bill Baxter wrote:
>>> In [35]: x = [ 0, 0, 0, 99, 0, 1, 5]
>>> In [37]: i=nonzero(x)
>>> In [38]: i
>>> Out[38]: (array([3, 5, 6]),)
> 
>> Just do i[0].  It's an array, not a string.  Try typing "type(i[0])"
>> and see what it tells you.
> 
> Which still begs the question: why does nonzero() return a tuple with an 
> array in it, rather than just the array?
> 
> Is it so you can so this?
> 
>  >>> a = numpy.array(((3,0,4),(5,21,0)))
> 
>  >>> numpy.nonzero(a)
> (array([0, 0, 1, 1]), array([0, 2, 0, 1]))
> 
>  >>> a[numpy.nonzero(a)]
> array([ 3,  4,  5, 21])

Yes, consistency between the 1-D case and the N-D case.

-- 
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


More information about the Numpy-discussion mailing list