[SciPy-user] Boolean Algebra

Alan G Isaac aisaac at american.edu
Tue Aug 21 11:45:18 EDT 2007


On Tue, 21 Aug 2007, Michael Hearne apparently wrote:
> I actually can't find any decent on-line tutorials 
> describing this, but booleans in Python are not done using 
> "&" and "|" symbols, but the words "and" and "or". 

This might not be the best answer to the OP's question.
See below.  Note that numpy's boolean matrices also
behavior very nicely.

Cheers,
Alan Isaac


>>> x= numpy.random.random((10,))
>>> y= numpy.random.random((10,))
>>> z1=x>0.1
>>> z2=x<0.2
>>> z1|z2
array([True, True, True, True, True, True, True, True, True, True], dtype=bool)
>>> z1 & z2
array([True, False, False, False, False, False, False, False, False, False], dty
pe=bool)
>>> z1 or z2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: The truth value of an array with more than one element is ambiguous.
 Use a.any() or a.all()
>>>





More information about the SciPy-User mailing list