From: Bill B. <wb...@gm...> - 2006-10-11 18:05:08
|
On 10/11/06, Nils Wagner <nw...@ia...> wrote: > Mark Bakker wrote: > > 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() > > > > ------------------------------------------------------------------------ > > > a[ (a>2) & (a<8) ] > & is bitwiase and which works fine for the booleans you get back from comparisons like (a>2). So in this case & is ok. For arrays with non-boolean values (any non-zero is True) use logical_and: a[ logical_and(c, d) ] Logical_and works always to give you the boolean result. '&' gives you the bitwise result, which is sometimes equivalent to the boolean result. --bb |