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

Find all rows of a numpy array that have different entries

For example:
For A = np.arange(9).reshape(3, 3),
I want to get

array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])

WITHOUT using loops

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

>Solution :

You can use:

B = np.sort(A, axis=1)

out = A[(B[:,:-1] != B[:, 1:]).all(1)]

Or using pandas:

import pandas as pd

out = A[pd.DataFrame(A).nunique(axis=1).eq(A.shape[1])]

Output:

array([[1, 2, 3]])
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