Numpy `logical_or` for more than two arguments

后端 未结 7 1010
庸人自扰
庸人自扰 2020-11-22 09:33

Numpy\'s logical_or function takes no more than two arrays to compare. How can I find the union of more than two arrays? (The same question could be asked wit

7条回答
  •  时光说笑
    2020-11-22 09:48

    As boolean algebras are both commutative and associative by definition, the following statements or equivalent for boolean values of a, b and c.

    a or b or c

    (a or b) or c

    a or (b or c)

    (b or a) or c

    So if you have a "logical_or" which is dyadic and you need to pass it three arguments (a, b, and c), you can call

    logical_or(logical_or(a, b), c)

    logical_or(a, logical_or(b, c))

    logical_or(c, logical_or(b, a))

    or whatever permutation you like.


    Back to python, if you want to test whether a condition (yielded by a function test that takes a testee and returns a boolean value) applies to a or b or c or any element of list L, you normally use

    any(test(x) for x in L)
    

提交回复
热议问题