[Numpy-discussion] multi-comparison expressions

Christopher Barker Chris.Barker@noaa....
Mon Oct 6 15:02:48 CDT 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.


Christopher Barker, Ph.D.

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


More information about the Numpy-discussion mailing list