Foreen
Foreen

Reputation: 399

Python Numpy - if array is in array

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.

Upvotes: 0

Views: 90

Answers (2)

Leonardo Viotti
Leonardo Viotti

Reputation: 506

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])

Upvotes: 1

joostblack
joostblack

Reputation: 2535

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

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

gives

False

Upvotes: 1

Related Questions