Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
209 views
in Technique[技术] by (71.8m points)

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.

question from:https://stackoverflow.com/questions/65922125/python-numpy-if-array-is-in-array

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...