I defined a custom data structure using namedtuple, which contains numpy array.
from collections import namedtuple
import numpy as np
AA = namedtuple('AA', 'name, x')
c = []
c.append(AA('x', np.arange(3)))
c.append(AA('x', np.arange(3)))
c.append(AA('y', np.arange(3)))
c[0] in c
c[1] in c
c[2] in c
Strangely, the c[1] test fails, whereas the other two works.
ValueError Traceback (most recent call last)
<ipython-input-70-c1daf83cd082> in <module>
----> 1 c[1] in c
ValueError: The truth value of an array with more than one element is ambiguous. Use c.any() or c.all()
The error seems to be related to numpy arrray equality test c[0].x == c[1].x. But for some reason the membership test for the first element always succeeds
s = []
s.append(np.arange(3))
s.append(np.arange(3))
s[0] in s
s[1] in s
Also see this example
class A:
def __eq__(self, other):
raise ValueError
a = [A(), A()]
a[0] in a
a[1] in a
Also I don't know why c[2] in c returns True.