对布尔列表中的元素布尔运算符是否有内置函数?

时间:2022-08-22 22:55:23

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 input lists have True, and False everywhere else.

例如,如果你有n个相同长度的bool列表,那么elementwise boolean AND应该返回另一个列表,该列表的长度在所有输入列表都为True的位置为True,其他地方为False。

It's pretty easy to write, i just would prefer to use a builtin if one exists (for the sake of standardization/readability).

这很容易编写,我只是希望使用内置(如果存在)(为了标准化/可读性)。

Here's an implementation of elementwise AND:

这是元素AND的实现:

def eAnd(*args):
    return [all(tuple) for tuple in zip(*args)]

example usage:

>>> eAnd([True, False, True, False, True], [True, True, False, False, True], [True, True, False, False, True])
[True, False, False, False, True]

5 个解决方案

#1


19  

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.

没有内置的方法来做到这一点。一般来说,列表推导等是你在Python中进行元素操作的方式。

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

Numpy确实在其数组类型中提供了这个(使用&,用于技术限制)。 Numpy数组通常按元素执行操作。

#2


14  

Try:

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

#3


2  

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

如果指定要折叠的维度,numpy.all函数会执行您想要的操作:

>>> 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)

#4


1  

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

不,没有这样的内置插件。你使用zip和all / any的方法是我会用的。

#5


1  

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.

不,我不相信标准库中有任何这样的功能......特别是当它根据提供的功能编写时非常容易。

#1


19  

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.

没有内置的方法来做到这一点。一般来说,列表推导等是你在Python中进行元素操作的方式。

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

Numpy确实在其数组类型中提供了这个(使用&,用于技术限制)。 Numpy数组通常按元素执行操作。

#2


14  

Try:

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

#3


2  

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

如果指定要折叠的维度,numpy.all函数会执行您想要的操作:

>>> 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)

#4


1  

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

不,没有这样的内置插件。你使用zip和all / any的方法是我会用的。

#5


1  

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.

不,我不相信标准库中有任何这样的功能......特别是当它根据提供的功能编写时非常容易。