1

I am beginning at numpy! Has numpy some function can search an array from another one ,and return the similar ones? Thanks!

import numpy as np

def searchBinA(B = ['04','22'],A):
    result = []
    ?......?  numpy.search(B,A)?   "is this correct?"
    return result

A = [['03', '04', '18', '22', '25', '29','30'], ['02', '04', '07', '09', '14', '29','30'], \
          ['06', '08', '11', '13', '17', '19','30'], ['04', '08', '22', '23', '27', '29','30'], \
          ['03', '05', '15', '22', '24', '25','30']]

print(str(searchBinA()))


output:[['03', '04', '18', '22', '25', '29','30'], ['04', '08', '22', '23', '27', '29','30']]
2
  • Are those elements in each row of A unique? Are elements in B unique? Commented Feb 27, 2016 at 10:23
  • yes!each element in each row of Ais unique ! but each row in A may not be unique! each element in b is also unique! Commented Feb 27, 2016 at 10:26

1 Answer 1

1

Assuming the inputs are NumPy arrays and that there are no duplicates within each row of A, here's an approach using np.in1d -

A[np.in1d(A,B).reshape(A.shape).sum(1) == len(B)]

Explanation -

  1. Get a mask of matches in A against any element in B with np.in1d(A,B). Note that this would be a 1D boolean array.

  2. Reshape the boolean array obtained from np.in1d(A,B) to A's shape and then look for rows that have n matches for each row, where n is the number of elements in B. Since, there are unique elements within each row, the rows with n matches are the rows we want in the final output.

  3. Therefore, sum the 2D reshaped boolean array along the rows and compare against n giving us a boolean mask, which when indexed into A would give us selective rows from it as the desired output.

Sample run -

In [23]: A
Out[23]: 
array([['03', '04', '18', '22', '25', '29', '30'],
       ['02', '04', '07', '09', '14', '29', '30'],
       ['06', '08', '11', '13', '17', '19', '30'],
       ['04', '08', '22', '23', '27', '29', '30'],
       ['03', '05', '15', '22', '24', '25', '30']], 
      dtype='|S2')

In [24]: B
Out[24]: 
array(['04', '22'], 
      dtype='|S2')

In [25]: A[np.in1d(A,B).reshape(A.shape).sum(1) == len(B)]
Out[25]: 
array([['03', '04', '18', '22', '25', '29', '30'],
       ['04', '08', '22', '23', '27', '29', '30']], 
      dtype='|S2')
Sign up to request clarification or add additional context in comments.

Comments

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.