[SciPy-user] question about logical "and" and ndarray in Scipy

Alan G Isaac aisaac at american.edu
Tue Nov 22 20:30:43 CST 2005


On Tue, 22 Nov 2005, "O'Keefe, Michael" apparently wrote: 
> After a little digging, I found that using an '==' would 
> be equivalent to the 'and' I had wanted to use. However, 
> just curious if the syntax in line 5 should have worked as 
> well. 

> [1]>>> import scipy 
> [2]>>> scipy.__scipy_version__ 
> '0.4.3' 
> [3]>>> scipy.__core_version__ 
> '0.6.1' 
> [4]>>> a=scipy.array([1,2,3,4,5,6],'d') 
> [5]>>> idx = scipy.where( (a>2) and (a<5), 1, 0 ) 

> Traceback (most recent call last): 
>   File "<pyshell#80>", line 1, in -toplevel- 
>     idx = scipy.where( (a>2) and (a<5), 1, 0 ) 
> ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() 
> [6]>>> idx = scipy.where( (a>2)==(a<5), 1, 0 ) 
> [7]>>> idx 
> array([0, 0, 1, 1, 0, 0]) 


If I understand your intent, your code illustrates the 
reason for the change: (a>2) and (a<5) would just return 
(a<5).  The change is to preclude such code. Use &.

hth,
Alan Isaac






More information about the SciPy-user mailing list