3

I want to compare two 1x3 arrays such as:

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

(output here is a 3x3x3 so output[x][y] is only a 1x3).

I'm getting an error that says:

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

Does that mean I need to do it like:

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

or is there a cleaner way to do this?

I'm using Python v2.6

4 Answers 4

8

The numpy way is to use np.allclose:

np.allclose(a,b)

Though for integers,

not (a-b).any()

is quicker.

Sign up to request clarification or add additional context in comments.

Comments

5

You should also get the message:

Use a.any() or a.all()

This means that you can do the following:

if (output[x][y] != [150,25,75]).all():

That is because the comparison of 2 arrays or an array with a list results in a boolean array. Something like:

array([ True,  True,  True], dtype=bool)

1 Comment

I think .any makes more sense for != and .all for ==.
3

convert to a list:

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

1 Comment

You can also compare two arrays of the same shape, which gives you an array of True/False values.
0

you could try:

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

if not all([i == j for i,j in zip(a, b)]):

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.