将两个布尔数组相交为True [英] Intersect two boolean arrays for True
本文介绍了将两个布尔数组相交为True的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
具有numpy数组
a = np.array([ True, False, False, True, False], dtype=bool)
b = np.array([False, True, True, True, False], dtype=bool)
如何使两者相交,以便仅True
值匹配?我可以做类似的事情:
how can I make the intersection of the two so that only the True
values match? I can do something like:
a == b
array([False, False, False, True, True], dtype=bool)
,但最后一项是True
(可以理解,因为两者都是False
),而我希望结果数组仅在第4个元素中为True
,例如:
but the last item is True
(understandably because both are False
), whereas I would like the result array to be True
only in the 4th element, something like:
array([False, False, False, True, False], dtype=bool)
推荐答案
Numpy提供了 logical_and()
为此:
Numpy provides logical_and()
for that purpose:
a = np.array([ True, False, False, True, False], dtype=bool)
b = np.array([False, True, True, True, False], dtype=bool)
c = np.logical_and(a, b)
# array([False, False, False, True, False], dtype=bool)
更多信息,请参见 Numpy逻辑运算.
这篇关于将两个布尔数组相交为True的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文