Search numpy array inside numpy array

Search numpy array inside numpy array

To get the same behavior as in for lists, you could do something like this:

any(np.all(row == m2) for row in m1)

That does the loop over rows in python, which isnt ideal, but it should work.

To understand whats going on with the numpy in, heres a description of the semantics of in from Robert Kern on the numpy mailing list:

It dates back to Numerics semantics for bool(some_array), which would
be True if any of the elements were nonzero. Just like any other
iterable container in Python, x in y will essentially do

for row in y:
   if x == row:
       return True
return False

Iterate along the first axis of y and compare by boolean equality. In
Numeric/numpys case, this comparison is broadcasted. So thats why
[3,6,4] works, because there is one row where 3 is in the first
column. [4,2,345] doesnt work because the 4 and the 2 are not in
those columns.

Probably, this should be considered a mistake during the transition to
numpys semantics of having bool(some_array) raise an exception.
scalar in array should probably work as-is for an ND array, but
there are several different possible semantics for array in array
that should be explicitly spelled out, much like bool(some_array).

Search numpy array inside numpy array

Leave a Reply

Your email address will not be published.