0

I want to check with most efficiency way (the fastest way), if some array (or list) is in numpy array. But when I do this:

import numpy

a = numpy.array(
    [
        [[1, 2]],
        [[3, 4]]
    ])

print([[3, 5]] in a)

It only compares the first value and returns True

Somebody knows, how can I solve it? Thank you.

3
  • 1
    Does this answer your question? Iterating over Numpy matrix rows to apply a function each? Commented Jan 27, 2021 at 15:26
  • Thank you, this helped me, too. Commented Jan 28, 2021 at 14:49
  • for your original data, is only the matrix a large or the query matrix is also large? Commented Feb 1, 2021 at 13:23

2 Answers 2

1

Your question seems to be a duplicate of: How to match pairs of values contained in two numpy arrays

In any case, something like the first answer should do it if I understand correctly:

import numpy

a = numpy.array(
    [
        [[1, 2]],
        [[3, 4]]
    ])

b = numpy.array([[3,5]])

print((b[:,None] == a).all(2).any(1))

Which outputs:

array([False,  True])
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! This works, it is faster than solution from joostblack, but it still takes a long time on my big data series. Is there any other faster solution? Thank you.
1

You could just add tolist() in your last line:

print([[3, 5]] in a.tolist())

gives

False

2 Comments

I do not know what your application is but I think you can drop the double brackets and instead use single brackets.
This works! Thank you, but it is really slow. I have big data series and with them it takes more than 1 minute. Is there any faster solution?

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.