【问题标题】:I need to remove every point that has the same Y coordinate in an array我需要删除数组中具有相同 Y 坐标的每个点
【发布时间】:2022-01-19 02:26:16
【问题描述】:

基本上我有一个数组列表 [x,y] : [0,1][1,2][2,4][3,1][4,3] 并且列表继续。我想执行一个代码,按顺序删除除第一个之外具有相同 y 坐标的点。我想输出:[0,1][1,2][2,4][4,3]。我怎么能做到这一点我已经尝试使用 np.unique 但我无法保持第一次出现或根据 y 坐标删除。

谢谢

【问题讨论】:

  • 您可以使用pandasdrop_duplicates
  • edit 添加实际数组。您在此处编写的内容无效,并且很难判断您是在谈论列表还是 NumPy 数组,但我认为它是一个数组,因此我为您添加了 numpy 标记。顺便说一句,欢迎来到 Stack Overflow!如果需要提示,请查看tourHow to Ask

标签: python numpy


【解决方案1】:

你可以使用HYRY's solution from numpy.unique with order preserved,你只需要选择Y列。

import numpy as np

a = np.array([[0,1], [1,2], [2,4], [3,1], [4,3]])

_, idx = np.unique(a[:, 1], return_index=True)
a[np.sort(idx)]

结果:

[[0 1]
 [1 2]
 [2 4]
 [4 3]]

【讨论】:

【解决方案2】:
array = [[0,1],[1,2],[2,4],[3,1],[4,3]]

occured = set()
result = []
for element in array:
    if element[1] not in occured:
        result.append(element)
    occured.add(element[1])
array.clear()
array.extend(result)
print(array)
>> [[0, 1], [1, 2], [2, 4], [4, 3]]

【讨论】:

  • 感谢您的帮助!!
猜你喜欢
  • 1970-01-01
  • 2021-05-30
  • 2012-09-23
  • 1970-01-01
  • 2012-01-14
  • 2018-07-08
  • 1970-01-01
  • 1970-01-01
  • 2021-09-04
相关资源
最近更新 更多