我有两个带有元组(坐标)的列表,例如:
some_pt1 = [(10.76,2.9),(3.24,4.28),(7.98,1.98),(3.21,9.87)]
some_pt2 = [(11.87,6.87), (67.87,8.88), (44.44, 6.78), (9.81, 1.09), (6.91, 0.56), (8.76, 8.97), (8.21, 71.66)]
>元组中的每个值都是一个单位
>列表长度不同
我要如何找到两个列表之间的两个最接近的点.我不知道怎么做,也许有可能用距离来做到.我希望有一种更有效的方法来执行此操作,因为我需要此功能尽快运行(这是更大功能的一部分).
解决方法:
或者,通过参考Tim Seed的代码.可以使用.
from scipy.spatial import distance
some_pt1 = [(10.76,2.9),(3.24,4.28),(7.98,1.98),(3.21,9.87)]
some_pt2 = [(11.87,6.87), (67.87,8.88), (44.44, 6.78), (9.81, 1.09), (6.91, 0.56), (8.76, 8.97), (8.21, 71.66)]
empthy_dict = {}
for i in range(len(some_pt1)):
for j in range(len(some_pt2)):
dist = distance.euclidean(some_pt1[i],some_pt2[j])
empthy_dict[dist] = [some_pt1[i],some_pt2[j]]
shortest = sorted(empthy_dict.keys())[0]
points = empthy_dict[shortest]
print('Shortest distance is ' ,shortest,' and points are ' ,points)