Skip to content Skip to sidebar Skip to footer

Are There Builtin Functions For Elementwise Boolean Operators Over Boolean Lists?

For example, if you have n lists of bools of the same length, then elementwise boolean AND should return another list of that length that has True in those positions where all the

Solution 1:

There is not a built-in way to do this. Generally speaking, list comprehensions and the like are how you do elementwise operations in Python.

Numpy does provide this (using &, for technical limitations) in its array type. Numpy arrays usually perform operations elementwise.

Solution 2:

Try:

[ x&y for(x,y) in zip(list_a, list_b)]

Solution 3:

The numpy.all function does what you want, if you specify the dimension to collapse on:

>>> all([[True, False, True, False, True], [True, True, False, False, True], [True, True, False, False, True]], 0)
array([ True, False, False, False,  True], dtype=bool)

Solution 4:

No, there are no such built-ins. Your method using zip and all / any is what I would use.

Solution 5:

No, I don't believe there's any such function in the standard library... especially when it's so easy to write in terms of the functions that are provided.

Post a Comment for "Are There Builtin Functions For Elementwise Boolean Operators Over Boolean Lists?"