0

If I have a list of tuples, the "in" function tells me exactly whether the tuple is in the list.

>>> a = [(1,2),(3,4)]

>>> (1,2) in a
True

>>> (1,3) in a
False

That's what I want. But when I do the same thing for b = np.array(a), both give True:

>>> b = np.array(a)

>>> (1,2) in b
True

>>> (1,3) in b
True

So it seems as though the in function for numpy arrays does not check the tuple anymore, but the single values.

  1. Why is this?
  2. How can I achive a truth request regarding whether a 2d element is in a numpy array as in the example above?
16
  • 3
    Operators work differently for all types. You would not expect + to work the same for integers, lists and strings, would you? Commented Dec 23, 2021 at 10:02
  • Ok sure, you're right. Then my question is the 2. only. Commented Dec 23, 2021 at 10:03
  • 1
    Related Commented Dec 23, 2021 at 10:05
  • Does this answer your question? "In" operator for numpy arrays? Commented Dec 23, 2021 at 10:11
  • Thanks! So it's clear it doesn't work for arrays as I need it. But what's the alternative for arrays? Looping through the single elements would take too much time, I suppose, would it not? I need to handle a lot of tuples (3.6e6 times 10) Commented Dec 23, 2021 at 10:13

0

Your Answer

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