【发布时间】:2016-09-12 14:35:12
【问题描述】:
如何对 numpy 数组执行“in”操作? (如果给定的 numpy 数组中存在元素,则返回 True)
对于字符串、列表和字典,功能直观易懂。
这是我将其应用于 numpy 数组时得到的结果
a
array([[[2, 3, 0],
[1, 0, 1]],
[[3, 2, 0],
[0, 1, 1]],
[[2, 2, 0],
[1, 1, 1]],
[[1, 3, 0],
[2, 0, 1]],
[[3, 1, 0],
[0, 2, 1]]])
b = [[3, 2, 0],
[0, 1, 1]]
b in a
True
#Aligned with the expectation
c = [[300, 200, 0],
[0, 100, 100]]
c in a
True
#Not quite what I expected
【问题讨论】:
-
对于一维数组有一个
np.in1d,但将其应用于二维数组的行需要一些技巧。查看它的代码以了解其中涉及的内容。
标签: python arrays numpy operators