python - 比较python中numpy数组的元素

标签 python arrays comparison numpy

我想比较两个 1x3 数组,例如:

if output[x][y] != [150,25,75]

(output 这里是 3x3x3 所以 output[x][y] 只是 1x3)。

我收到一条错误消息:

ValueError: The truth value of an array with more than one element is ambiguous. 

这是否意味着我需要这样做:

if output[y][x][0] == 150 and output[y][x][1] == 25 and output[y][x][2] == 75:

或者是否有更简洁的方法来做到这一点?

我正在使用 Python v2.6

最佳答案

numpy 的方法是使用 np.allclose :

np.allclose(a,b)

虽然对于整数,

not (a-b).any()

更快。

关于python - 比较python中numpy数组的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6628793/

相关文章:

javascript - 增进理解 - 在循环内使用扩展运算符复制数组

arm - A64 Neon SIMD - 256 位比较

python - CIFAR-10 无意义的归一化值

python - 在路径中包含 Python 库?

arrays - 在 Matlab 中过滤数组/向量

java - 伪代码数组符号混淆

python - 根据索引分配值的最快方法

python - 如何使 __add__ 的 numpy 重载独立于操作数顺序?

c++ - std::lower_bound 的比较函数

python - 比较嵌套列表的相似度