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

>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]])

Leave a Reply