[英]How to apply bitwise operator to compare a list of objects
Suppose I have a long list of objects (say, a list of numpy matrices of bool elements) foo = [a, b, c]
, that I want to compare with some bitwise operator, to get something like a | b | c
假设我有一个很长的对象列表(例如,bool 元素的 numpy 矩阵列表)
foo = [a, b, c]
,我想与一些按位运算符进行比较,以获得类似a | b | c
a | b | c
a | b | c
. a | b | c
。
If I could use this bitwise operation as a function, say a bitwiseor
function, I could simply do this with bitwiseor(*foo)
.如果我可以使用这个按位运算作为一个函数,比如说一个
bitwiseor
函数,我可以简单地用bitwiseor(*foo)
来做到这一点。 However, I was not able to find whether the bitwise or can be written in such functional form.但是,我无法找到是否可以按位或以这种函数形式编写。
Is there some handy way to handle this kind of problem?有没有一些方便的方法来处理这种问题? Or should I just use a loop to compare all the elements cumulatively?
或者我应该只使用循环来累积比较所有元素?
Using the functional method in operator
in combination withfunctools.reduce
:结合
functools.reduce
使用operator
中的函数方法:
>>> import operator, functools
>>> functools.reduce(operator.or_, [1, 2, 3])
3
numpy
has a number of functions that support reducing along an axis. numpy
有许多支持沿轴减少的函数。 See numpy.bitwise_or
and numpy.bitwise_or.reduce
.请参阅
numpy.bitwise_or
和numpy.bitwise_or.reduce
。
np.bitwise_or.reduce(your_array)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.