【发布时间】:2022-01-18 19:43:46
【问题描述】:
举个例子。我有一个数组test,并且想要为所有等于ref 的元素的元素获取一个带有True 的布尔掩码。
import numpy as np
test = np.array([[2, 3, 1, 0], [5, 4, 2, 3], [6, 7, 5 ,4]])
ref = np.array([3, 4, 5])
我正在寻找类似的东西
mask = (test == ref[0]) | (test == ref[1]) | (test == ref[2])
在这种情况下应该产生
>>> print(mask)
[[False, True, False, False],
[ True, True, False, True],
[False, False, True, True]]
但不必求助于任何循环。
【问题讨论】: