Numpy `logical_or` for more than two arguments

后端 未结 7 996
庸人自扰
庸人自扰 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:53

    I use this workaround which can be extended to n arrays:

    >>> a = np.array([False, True, False, False])
    >>> b = np.array([True, False, False, False])
    >>> c = np.array([False, False, False, True])
    >>> d = (a + b + c > 0) # That's an "or" between multiple arrays
    >>> d
    array([ True,  True, False,  True], dtype=bool)
    

提交回复
热议问题