I have two boolean lists, e.g.,
x=[True,True,False,False]
y=[True,False,True,False]
I want to AND these lists together, with the expected o
You could use numpy:
>>> import numpy as np
>>> x=np.array([True,True,False,False])
>>> y=np.array([True,False,True,False])
>>> x & y
array([ True, False, False, False], dtype=bool)
Numpy allows numerical and logical operations on arrays such as:
>>> z=np.array([1,2,3,4])
>>> z+1
array([2, 3, 4, 5])
You can perform bitwise and with the &
operator.
Instead of a list comprehension, you can use numpy to generate the boolean array directly like so:
>>> np.random.random(10)>.5
array([ True, True, True, False, False, True, True, False, False, False], dtype=bool)