Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
100 views
in Technique[技术] by (71.8m points)

python - how to make my numpy array unique by axis?

i have a numpy array like the XY coordinates here below:

2d_coords = [
[1,2]
[1,1]
[2,1]
[3,1]
...
]

either [1,1] or [1,2] need to go (doesn't care which one) , only one point on the X coordinate is possible. How can I do that ?

question from:https://stackoverflow.com/questions/65870206/how-to-make-my-numpy-array-unique-by-axis

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

numpy.unique would be helpful. For example,

import numpy as np

l = np.asarray([
    [1, 2],
    [1, 1],
    [2, 1],
    [3, 1],
])

_, unique_indices = np.unique(l[:, 0], return_index=True)  # get the indices with unique x coordinates
print(l[unique_indices])

The example output:

[[1 2]
 [2 1]
 [3 1]]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...