python - 从列表中删除相似的项目

标签 python python-3.x duplicates python-3.6

从元组列表开始,其中包含图形上点的 x 和 y 坐标;我想删除列表中的重复点;然而,出于我的目的,距离 10 以内的点我认为是重复的。

我已经编写了一个似乎可以完成这项工作的函数,但我敢打赌有更好的方法。在下面的示例数据中:点 1、2 和 5 是重复的(彼此的距离在 10 以内)。我不在乎这三点中哪一点能在淘汰过程中幸存下来。我预计处理的点不会超过 100 个,其中大约 50% 会被淘汰。谢谢!

def is_close(pointA, pointB, closeness):
    x1, y1  = pointA
    x2, y2 = pointB
    distance = int(((x2-x1)**2 + (y2-y1)**2)**0.5) # distance formula
    if distance < closeness:
        return True
    return False

def remove_close_duplicated(data, closeness):
    if len(data) < 2: # can't have duplicates if there aren't at least 2 points
        return data
    new_list_points = []
    for i, point in enumerate(data):
        if i == 0:
            new_list_points.append(point)
            continue
        close = False
        for new_point in new_list_points:
            if is_close(new_point, point, closeness):
                close = True
                break 
        if close == False:
            new_list_points.append(point)
    return new_list_points

sample_data =[
    (600, 400), # 1
    (601, 401), # 2
    (725, 300), # 3
    (800, 900), # 4
    (601, 400), # 5
]

closeness = 10                  
print(remove_close_duplicated(sample_data, closeness))
'''
output is:
[(600, 400), (725, 300), (800, 900)]
'''

最佳答案

这有两个部分:查找紧密对和查找分离良好的集合(近邻关系的传递闭包的等价类,或近邻图的连通分量)。

只有 100 个点,您可以通过蛮力完成第一部分,但有效的选择包括将一侧分组为 10 个容器,这样一个点的所有近邻都必须位于其容器或相邻容器中,或者将点存储在k-d树中。

对于第二部分,一个标准解决方案是构建一个不相交集森林,在每个相邻对之间应用并集运算(任意选择一个点存储在(新)根中)。与末尾的根相关联的点是所需的约简集。

关于python - 从列表中删除相似的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52864544/

相关文章:

python - 如何让一系列事件反射(reflect)这些事件在现实生活中的概率而不重复发生?

python - 如何使用 Selenium 在动态加载的网页中正确滚动?

regex - 如何删除重复的字符并仅在 Perl 中保留唯一的字符?

python - 从字典中删除重复值而不删除键

r - Distinct (dplyr) 效果不佳 - 基于标准的独特观察

python - 通过循环文本文件找到字符串后进入下一次迭代?

python - 使用 skimage 从多边形中提取图像片段

python - 如何在python中创建使用相同值初始化的特定类型的指定维度数组?

python - networkx 中的 nx.get_node_attributes 返回一个空字典

python-3.x - 我收到语法错误和变量注释非法目标的错误。我如何解决它?