[Numpy-discussion] multi-comparison expressions

Christopher Barker Chris.Barker at noaa.gov
Mon Oct 6 16:02:48 EDT 2008


John wrote:
> hi,
> 
> why does the ValueError appear below,

because python short circuits this expression, and numpy can't overwrite 
that -- the same reason a and b doesn't work for a and b arrays.

 > and how can i make that 2<a<5
> expression work when a is an array?

 >>> (a > 2) & (a < 5)

array([[False, False, False],
        [ True,  True, False],
        [False, False, False]], dtype=bool)

& is the bitwise-and, which is overridden by numpy, and works like and 
for boolean data.

-Chris



-- 
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (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