比较python中numpy数组的元素

比较python中numpy数组的元素,python,arrays,comparison,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] ==

我想比较两个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将以下内容转换为列表:

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

您还应该得到以下信息:

使用a.any()或a.all()

这意味着您可以执行以下操作:

if (output[x][y] != [150,25,75]).all():
这是因为比较两个数组或一个数组与一个列表会得到一个布尔数组。比如:

array([ True,  True,  True], dtype=bool)
你可以试试:

a = output[x][y]
b = [150,25,75]

if not all([i == j for i,j in zip(a, b)]):
最基本的方法是使用:

虽然对于整数

not (a-b).any()

更快。

您还可以比较相同形状的两个数组,这将为您提供一个真/假值数组。我认为。对于
,任何值都更有意义=和.all用于
==
not (a-b).any()