元素是否存在于 numpy 数组中?

Does element exist in numpy array?

numpy 新手。

如何最好地查看一个元素是否存在于 numpy 数组中?

示例:

import numpy as np
a = np.array([[1, 2], [2, 3], [3, 4]])
[2, 4] in a
# This evaluates to True because there's 
# a 2 (somewhere) and a 4 (somewhere)
# but I want to match [2, 4] ONLY. So...
[2, 4] in a  # Would like this to be False
[2, 3] in a  # Would like this to be True
[3, 2] in a  # This too should be false (wrong order [3, 2] != [2, 3])

我查看了 np.where(),这似乎不是我要找的东西。我使用 np.isin([2, 4], a) 得到与上面类似的结果。

不需要索引(尽管如果它随行就可以),只需一个布尔值就足够了。

您正在搜索 a 中的 2 和 4,尝试:

[[2, 4]] in a

这被标记为重复 testing whether a Numpy array contains a given row wherein Tom10 提供了我寻求的答案:

np.equal([2, 3], a).all(axis=1).any()