One thing you need to understand, in python in general, is that, semantically, __contains__
is based on __eq__
, i.e. it looks for an element which satisfies the ==
predicate. (Of course one can override the __contains__
operator to do other things, but that's a different story).
Now, with numpy arrays, the __eq__
does not return bool
at all. Every person using numpy encountered this error at some point:
if temp == temp2:
print 'ok'
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Which means, given the conflicting semantics of the __contains__
and ndarray.__eq__
, it's not surprising this operation does not do what you want.
With the code you posted, there is no clear advantage to setting temp
to be a np.array
over a list
. In either case, you can "simulate" the behavior of __contains__
with something like:
temp2 = np.array([0,1])
any( (a == temp2).all() for a in temp if a is not None )
If you explain why you choose to use an hetrogeneous np.array
in the first place, I might come up with a more detailed solution.
And, of course, this answer would not be complete without @user2357112's link to this question.