Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python: comparing numpy array with sub-numpy array without loop

My problem is quite simple but I cannot figure how to solve it without a loop.

I have a first numpy array:

FullArray = np.array([0,1,2,3,4,5,6,7,8,9])

and a sub array (not necessarily ordered in the same way):

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Sub array = np.array([8, 3, 5])

I would like to create a bool array that has the same size of the full array and that returns True if a given value of FullArray is present in the SubArray and False either way.

For example here I expect to get:

BoolArray = np.array([False, False, False, True, False, True, False, False, True, False])

Is there a way to do this without using a loop?

>Solution :

You can use np.isin:

np.isin(FullArray, SubArray)
# array([False, False, False, True, False, True, False, False, True, False])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading