Combining 3 boolean masks in Python

bard

I have 3 lists:

a = [True, False, True]
b = [False, False, True]
c = [True, True, False]

When I type

a or b or c

I want to get back a list that's

[True, True, True]

but I'm getting back

[True, False, True]

Any ideas on why? And how can I combine these masks?

jscs

Your or operators are comparing the lists as entire objects, not their elements. Since a is not an empty list, it evaluates as true, and becomes the result of the or. b and c are not even evaluated.

To produce the logical OR of the three lists position-wise, you have to iterate over their contents and OR the values at each position. To convert a bunch of iterables into a list of their grouped elements, use zip(). To check if any element in an iterable is true (the OR of its entire contents), use any(). Do these two at once with a list comprehension:

mask = [any(tup) for tup in zip(a, b, c)]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Combining boolean masks, why is in Python: [False, True] and [True, False] == [True, False]

Is there a way to broadcast boolean masks?

Boolean array masks in Javascript

Emulating boolean masks in Theano

Python3 and combining Diacritics

slicing numpy arrays by combining indices and expression masks

Pygame masks Python

creating a new dataframe using boolean masks

Combining multiple txt files (Python 3, UnicodeDecodeError)

Combining custom comparison with key selection in python 3

Python 3 - Assert a boolean.

Object detection with Keras - issue with combining training images and target masks

PHP combining boolean operators (AND, OR) in an if statement

Combining logical (boolean) expressions in numpy

Overlaying segmentation masks with preference in Python

Efficiently creating masks - Numpy /Python

Python3: Creating two Masks vs. One In-line mask -> Different Results?

Python: Create a 3d mask from 2d masks

To get the center mask from an image of masks using Python3 and opencv

How to collect a set of boolean values and return a final test combining all with 'AND' in Python?

Split array to multiple arrays using a collection of boolean masks

Pandas: Replace values of multiple columns using boolean masks

Filter list elements in a Series of lists by a series of boolean masks

Using df.loc[] vs df[] shorthand with boolean masks, pandas

Construct Boolean masks based on unknown number of columns and values

How does numPy's advanced indexing work for boolean masks?

When indexing a DataFrame with a boolean mask, is it faster to apply the masks sequentially?

False Boolean is not showing in proto3 Python

A question about identity and boolean in Python3