[英]Python - Library function that given a X,Y pair of point find Xn, Yn which is the closest pair to that pair
[英]python find the closest point from two tables and print the pair names
我假设您想要 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.