0

How do I code which items in array 1 are contained within array 2, and append a row reflecting this (1 = it is contained, 0 = it is not:

import numpy as np
array1 = np.array([1,2,3,4,5,10,12,13])

array2 = np.array([4,6,3,5,0])

Expected Result:

np.array([[1,2,3,4,5,10,12,13],
           [0,0,1,1,1,0,0,0]])
1
  • use np.isin() method Commented Jul 30, 2020 at 9:04

1 Answer 1

1

Use np.isin and cast the result to integer, the solution is within the previous answer :)

np.vstack([array1, np.isin(array1, array2).view('i1')])
array([[ 1,  2,  3,  4,  5, 10, 12, 13],
       [ 0,  0,  1,  1,  1,  0,  0,  0]])
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.