[SciPy-user] Array functions

Robert Kern rkern at ucsd.edu
Mon Aug 8 06:21:19 CDT 2005


Howey, David A wrote:
> I guess this is a numpy question, but how do I do truth tests on arrays?
> I'm trawling around the documentation but struggling to find an answer..
> 
> I want to test if any array contains ONLY zeros.
> 
> Originally I had:
>  if a == 0:
> 	# code in here
> 
> I need this to be true both for 
> a = 0 # ie a is just a number
> 
> and for
> a = array((0,0,0,0,0))
> 
> It must return false for any array with any nonzero elements anywhere

In [1]: a = array((0,0,0,0,0))

In [2]: alltrue(a == 0)
Out[2]: 1

In [3]: a == 0
Out[3]: array([1, 1, 1, 1, 1],'b')

In [4]: a[1] = 10

In [5]: alltrue(a == 0)
Out[5]: 0

In [6]: a == 0
Out[6]: array([1, 0, 1, 1, 1],'b')

There's also a sometrue() function, too.

-- 
Robert Kern
rkern at ucsd.edu

"In the fields of hell where the grass grows high
  Are the graves of dreams allowed to die."
   -- Richard Harter



More information about the SciPy-user mailing list