0

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)

3 Answers 3

2

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))
Sign up to request clarification or add additional context in comments.

1 Comment

you can also use list(dict.fromkeys(x)) for your order preserving solution.
1

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]

Comments

1

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.