Reputation: 482
I am storing 3d coordinates in three arrays. Below follows an example of a total of four entries of coordinate points, of the coordinates (1,2,3), (2,3,4) and (3,4,5)
, where the first coordinate point is duplicated.
x = [1, 2, 3, 1]
y = [2, 3, 4, 2]
z = [3, 4, 5, 3]
Now, I would like to as efficiently as possible remove duplicate coordinates, such that the above arrays result in
x_prime = [1, 2, 3]
y_prime = [2, 3, 4]
z_prime = [3, 4, 5]
...still containing the same coordinates (1,2,3), (2,3,4) and (3,4,5)
however with no duplicate entry of (1,2,3)
Upvotes: 0
Views: 642
Reputation: 304
In pure python:
x_prime = list(dict([e, 0] for e in x).keys())
EDIT:
Mustafa Aydin's solution in the comments is even simpler :
list(dict.fromkeys(x))
Upvotes: 2
Reputation: 1431
I guess this is a simple, pythonic and linear solution of your problem. Please let me know the performance.
l = []
for a, b, c in zip(x, y, z):
l.append((a, b, c))
x_prime = []
y_prime = []
z_prime = []
for a, b, c in set(l):
x_prime.append(a)
y_prime.append(b)
z_prime.append(c)
Upvotes: 1
Reputation: 215047
You can use np.unique(..., axis=0)
:
np.unique(np.array([x, y, z]).T, axis=0)
# [[1 2 3]
# [2 3 4]
# [3 4 5]]
If you need to unpack the result:
x_prime, y_prime, z_prime = np.unique(np.array([x, y, z]).T, axis=0)
x_prime
# [1 2 3]
y_prime
# [2 3 4]
z_prime
# [3 4 5]
Upvotes: 1