[Numpy-discussion] conditionals not fully implemented?

Tim Hochberg tim.hochberg at cox.net
Mon Feb 2 16:44:42 CST 2004


Chris Barker wrote:

>
>
> Chris Burns wrote:
>
>> I'm puzzled by the following behaviour when using conditionals
>> in a function like nonzero():
>>
>>>>> numarray.nonzero(x < 5 and x > 2)
>>>>
>>
>> Am I doing something
>> wrong or is the use of 'and' and 'or' not implemented for conditionals?
>
>
> "and" and "or" are not implimented because Python does not allow them 
> to be overloaded. Searching of various archives might tell you why. 
> One work around is to use bitwise-and : "&". It means something 
> different, but if you're comparing just ones and zeros, it works fine:
>
>  x = numarray.arange(10)
>
> >>> x = numarray.arange(10)
> >>> numarray.nonzero( (x < 5) & (x > 2) )
> (array([3, 4]),)
> >>>
>
> bitwise or seems to work fine too:
>
> >>> numarray.nonzero( (x < 2) | (x > 5) )
> (array([0, 1, 6, 7, 8, 9]),)
> >>>
>
> Make sure you use enough parenthesis.
>
> -Chris
>

And, if it turns out you are not using just ones and zeros, investigate 
logical_or and friends. You may also want to look at bitwise_or and 
friends as well although they are just synonyms for '|, etc..

-tim








More information about the Numpy-discussion mailing list