簡體   English   中英

python 從兩個表中找到最近的點並打印對名稱

[英]python find the closest point from two tables and print the pair names

您好,我是 python 的新手,在查看我的問題的解決方案時,我只從一個列表中找到了選擇。 在我的例子中有兩個表

表格1:

表格1

表 2:

表 2

我想 select 是兩個表中最接近的點對,而 output 是獲取點對的列表。 誰能幫助我?

我假設您想要 table1 中的一個點和 table2 中最接近的一個點。

你可以試試這個..

import math

# finds the Euclidean distance
def getDistance(x1,y1,x2,y2):
    return math.sqrt((x1-x2)**2+(y1-y2)**2)

#finds the closest points from table1 and table2
def getClosestPoints(table1, table2):
    #pointA, pointB
    distance = None
    for x1,y1 in zip(table1['x'], table1['y']):
        for x2, y2 in zip(table2['x'], table2['y']):
            dis = getDistance(x1,y1,x2,y2)
            #print (f"x1 = {x1}, y1 = {y1}, x2 = {x2}, y2 = {y2} | dis = {dis}")
            if distance == None or distance > dis:
                distance = dis
                pointA = (x1,y1)
                pointB = (x2,y2)
                #print(f"A={pointA}, B={pointB}, dis = {distance}")
            else:
                continue
    return pointA, pointB, distance

返回點 1、點 2 和距離。

print(getClosestPoints(table1, table2))

輸出:((41.31175,15.1337),(41.55,14.96778),0.2903317221730979)

這是((x1,y1),(x2,y2),距離)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM