Paymahn Moghadasian - 1 year ago 70

Python Question

I'm trying to check the existence of an array in a list of arrays. My problem can be boiled down to the example below:

`>>> import numpy as np`

>>> a = np.array([1,2,3])

>>> b = np.array([4,5,6])

>>> c = [a,b]

>>> c

[array([1, 2, 3]), array([4, 5, 6])]

>>> a in c

True

>>> np.array([1,2,3]) in c

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

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

>>> d = np.array([1,2,3])

>>> d in c

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

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

How can I check the existence of an array in a list of arrays? This works as I'd expect when I use regular lists:

`>>> a = [1,2,3]`

>>> b = [4,5,6]

>>> c = [a,b]

>>> [1,2,3] in c

True

>>> [4,5,6] in c

True

>>> [1,2] in c

False

>>> d = [1,2,3]

>>> d in c

True

Answer Source

easy

```
import numpy as np
a = np.array([1,2,3])
b = np.array([4,5,6])
c = np.array([a,b]) # < ---- this line!
print repr(c)
print a in c
print np.array([1,2,3]) in c
print [1,2,3] in c # Success!!!
print np.array([1,2,3]).tolist() in c# also success!!!
```