select part of array using two conditions

Francesc Altet faltet at carabos.com
Wed Oct 11 04:20:50 CDT 2006


El dc 11 de 10 del 2006 a les 11:06 +0200, en/na Mark Bakker va
escriure:
> Hello -
> 
> I want to select part of an array using two conditions.
> I know how to do it with one condition (and it works great), but when
> I use two conditions I get an error message?
> This is probably easy, but I cannot figure it out. 
> Thanks for any help, Mark
> 
> >>> a = arange(10)
> >>> a
> array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
> >>> a[ a>2 ]
> array([3, 4, 5, 6, 7, 8, 9])
> >>> a[ a>2 and a<8 ] 
> Traceback (most recent call last):
>   File "<pyshell#52>", line 1, in ?
>     a[ a>2 and a<8 ]
> ValueError: The truth value of an array with more than one element is
> ambiguous. Use a.any() or a.all()

Yeah, this is a common error on people that is not used to these kind of
selections. You have to use the boolean binary operators (and not the
logical operators) for doing this:

>>> a[(a>2) & (a<8)]

Notice the parenthesis. They are necessary because the operator & has
more precedence than < or >.

-- 
>0,0<   Francesc Altet     http://www.carabos.com/
V   V   Cárabos Coop. V.   Enjoy Data
 "-"



-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Numpy-discussion mailing list
Numpy-discussion at lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/numpy-discussion


More information about the Numpy-discussion mailing list