[Numpy-discussion] conditionals not fully implemented?

Chris Barker Chris.Barker at noaa.gov
Mon Feb 2 16:28:49 CST 2004


Chris Burns wrote:
> I'm puzzled by the following behaviour when using conditionals
> in a function like nonzero():
>>>>numarray.nonzero(x < 5 and x > 2)
> 
> Am I doing something
> wrong or is the use of 'and' and 'or' not implemented for conditionals?

"and" and "or" are not implimented because Python does not allow them to 
be overloaded. Searching of various archives might tell you why. One 
work around is to use bitwise-and : "&". It means something different, 
but if you're comparing just ones and zeros, it works fine:

  x = numarray.arange(10)

 >>> x = numarray.arange(10)
 >>> numarray.nonzero( (x < 5) & (x > 2) )
(array([3, 4]),)
 >>>

bitwise or seems to work fine too:

 >>> numarray.nonzero( (x < 2) | (x > 5) )
(array([0, 1, 6, 7, 8, 9]),)
 >>>

Make sure you use enough parenthesis.

-Chris




-- 
Christopher Barker, Ph.D.
Oceanographer
                                     		
NOAA/OR&R/HAZMAT         (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov





More information about the Numpy-discussion mailing list