How can I get the unique rows of an array while preserving the order (of first appearance) of the rows in the result?
The below code tried with variations resulting in a single array.
array_equal
compares with element positions.
import numpy as np
unique = np.array([])
arr = np.array([[1,2,3,4,5],[3,4,5,6,7],[5,6,7,8,9],[7,8,9,0,1],[9,0,1,2,3],[1,2,3,4,5],[-8,-7,-6,-5,-4]])
u = 0
for idx, i in enumerate(arr):
if np.array_equal(unique, i) == False:
unique = np.append(unique, i, axis=None)
u += 1
print (unique)
print(u)
>>> print (unique)
[ 1. 2. 3. 4. 5. 3. 4. 5. 6. 7. 5. 6. 7. 8. 9. 7. 8. 9.
0. 1. 9. 0. 1. 2. 3. 1. 2. 3. 4. 5. -8. -7. -6. -5. -4.]
>>> print(u)
7
>>>
For this example, the expected result is an array with 6 unique rows.
[[1,2,3,4,5],[3,4,5,6,7],[5,6,7,8,9],[7,8,9,0,1],[9,0,1,2,3],[-8,-7,-6,-5,-4]]