0

# Bitwise AND(&) for numpy array - [True,True] and [True,False]

Why should we use & (bitwise AND operator) when working with two numpy arrays? At the same time logical AND is enough when working with two lists. For example: a=[True,True] b=[True,False] >> a and b: [True, False] >> b and a: [True, True] # why?? If we create the same numpy arrays: a_arr=np.array(a) b_arr=np.array(b) >> a_arr and b_arr ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() >>a_arr & b_arr array([ True, False]) >>b_arr & a_arr array([ True, False])

1 Answer

+ 2

1) a and b will print b, b and a will print b
When you use and between lists, then it will return the last list.
For example:
e = a and b # assigns b to e
d = b and a # assigns a to d
Boolean operations work differently on lists, but there are built in functions like the any() and all(), for example:
all(a) and all(b) == all(b) and all(a)
2) I don't know much about numpy, but maybe it doesn't even allow boolean comparison between it array objects, to remove confusion