0

I am storing numpy.ndarray as a value of a dictionary. My dictionary looks like:

{0: array([[ 1,  0, -1],
   [ 0, -1,  0],
   [ 1,  0,  0]])}

But when I am trying to find out if array exists in the dictionary using the following command

S = np.zeros((3,3), dtype=int)
if S in myDict.values():
   print "something"

I am getting the following error:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

I have seen some one who have this problem, but their situation is different. Can anybody help me get ouf it?

At least any suggestion to check if a value exist in Dictionary? Thank you in advance.

1
  • What should be the command exactly? Commented May 4, 2014 at 1:54

1 Answer 1

1

You should use np.array_equal:

S = np.zeros((3,3), dtype=int)
if any(np.array_equal(S, i) for i in myDict.values()):
   print "Yes!"
else:
   print "Sorry. No."

[OUTPUT]
Sorry. No.
Sign up to request clarification or add additional context in comments.

2 Comments

@vessilli, Glad I could help. If my answer was helpful, would you mind accepting it? (After the time limit) Thank you. And good luck.
Why not!! I will do it after the time limit.

Your Answer

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