From: Francesc A. <fa...@ca...> - 2006-10-11 09:21:01
|
El dc 11 de 10 del 2006 a les 11:06 +0200, en/na Mark Bakker va escriure: > Hello - >=20 > 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.=20 > Thanks for any help, Mark >=20 > >>> a =3D 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 ]=20 > 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 >. --=20 >0,0< Francesc Altet http://www.carabos.com/ V V C=C3=A1rabos Coop. V. Enjoy Data "-" |