0

I am trying to check if the formatted vector a_and_b[::2] is equivalent to a, but it is giving me an error ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all(). How would I be able to fix that and get the Expected Output?

import numpy as np 

a = np.array([5,32,1,4])
b = np.array([1,5,11,3])
a_and_b = np.array([5,1,32,5,1,11,4,3])
result = 'yes'  if a_and_b[::2] == a else 'no'

Expoected output:

yes
1
  • look at a_and_b[::2] == a before trying to use it Commented Nov 13, 2021 at 0:33

1 Answer 1

1

You probably want to use this:

(a_and_b[::2] == a).all()

which will return True if all of the elements of each array are equal, since:

>>> a_and_b[::2] == a
array([ True,  True,  True,  True])

returns an array of True/False. all() will indicate whether all the elements of that array are True or not.

So try:

result = 'yes' if (a_and_b[::2] == a).all() else 'no'
Sign up to request clarification or add additional context in comments.

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.