I have an arbitrary number of lists that I want to take the boolean &
of. For example for 2 lists, I have
x = [0, 1, 0, 0]
y = [1, 1, 0, 1]
[np.array(x) & np.array(y) for x,y in zip(x, y)]
[0, 1, 0, 0]
for 3 lists
z=[0,1,1,1]
I would have
[np.array(x) & np.array(y) & np.array(y) for x,y,z in zip(x, y, z)]
[0, 1, 0, 0]
etc.,
since my I have an arbitrary number of lists over which to perform this operation, what is the best method to achieve this?
You can use zip
and all
:
x = [0,1,0,0]
y = [1,1,0,1]
z = [0,1,1,1]
output = [all(zipped) for zipped in zip(x, y, z)]
print(output) # [False, True, False, False]
If you do want to get [0,1,0,0]
instead, use int(all(zipped))
instead of all(zipped)
(but this explicit recasting is redundant in most cases).