18

I've got a list (used as a stack) of numpy arrays. Now I want to check if an array is already in the list. Had it been tuples for instance, I would simply have written something equivalent to (1,1) in [(1,1),(2,2)]. However, this does not work for numpy arrays; np.array([1,1]) in [np.array([1,1]), np.array([2,2])] is an error (ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()). The error message does not help here AFAIK, as it is referring to comparing arrays directly.

I have a hard time beliving it wouldn't be possible, but I suppose there's something I'm missing.

1
  • I have a hard time believing the simplest method requires 2 function calls and 1 list comprehension... This functionality seems common enough to warrant it's own built-in function Commented Jan 27, 2017 at 3:31

5 Answers 5

29

To test if an array equal to a is contained in the list my_list, use

any((a == x).all() for x in my_list)
Sign up to request clarification or add additional context in comments.

6 Comments

I used this, though I had to make it a list comprehension (np.any([(a == x).all() for x in my_list])) for it to work. My thinking is that it is otherwise a generator? If my_list is empty, the original returns True.
any() in my answer is the Python built-in any(), and not numpy.any(). These are different functions! Don't use from numpy import * -- it will overwrite several Python built-ins.
Oh. I haven't imported everything, but I did not know there was a python builtin called any, so I assumed you meant numpy.any. Sorry about the misunderstanding.
It might be better to use the optimized numpy internal array_equal() function. any(np.array_equal(a, x) for x in my_list)
@fabianegli Your comment makes it sound like this function could be faster, while in fact the implementation of that function is essentially identical to the code in this answer, so you only add more function call overhead by using that function. It might be the right choice anyway, for a different reason: == will throw an exception if the arrays have incompatible shapes, while array_equal() will simply return False, which may or may not be what you want.
|
2

If you are looking for the exact same instance of an array in the stack regardless of whether the data is the same, then you need to this:

id(a) in map(id, my_list)

Comments

2

Sven's answer is the right choice if you want to compare the actual content of the arrays. If you only want to check if the same instance is contained in the list you can use

any(a is x for x in mylist)

One benefit is that this will work for all kinds of objects.

Comments

0

What about this:

a = array([1, 1])

l = [np.array([1,1]), np.array([2,2])]
list(map(lambda x: np.array_equal(x, a), l)

[True, False]

Comments

-1

You can convert the array into a list by using tolist() and then do the check:

my_list = [[1,1], [2,2]]

print(np.array([1,1]).tolist() in my_list)
print(np.array([1,2]).tolist() in my_list)

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.