简体   繁体   English

使用 Python 中的欧几里得距离确定最近位置

[英]Determine closest location using euclidian distance in Python

So I'm struggling to find the closest Euclidean distance of two coordinates from data in a dictionary.所以我正在努力从字典中的数据中找到两个坐标的最近欧几里得距离。
First, I have figured out how to compute the distance between two Cartesian coordinates (x,y) using the following:首先,我已经弄清楚如何使用以下方法计算两个笛卡尔坐标 (x,y) 之间的距离:

from math import sqrt
def distance(loc1_coordinates, loc2_coordinates):
    point1x, point1y = loc1_coordinates
    point2x, point2y = loc2_coordinates
    Distance = sqrt((point1x-point2x)**2 + (point1y-point2y)**2)  
    return "The distance between this two points is", str(round(Distance, 14))+" units"

print(distance([0,0],[3,4])) # distance should be 5.0

How to create a new function based on my earlier function so that I get the following as a result?如何根据我之前的 function 创建一个新的 function 以便我得到以下结果?

cities = {'A':[5,21], 'B':[43,76], 'C':[56,19], 'D':[21,37], 'E':[76,23], 'F':[45,56], 'G':[23,13]}
print(closest_destination('A', cities)) # should be 'G'

Update: I am trying to find the smallest distance in the list of calculations of the inputted City: eg: comparing A->B, A->C, A->D, ... and pick the one who has the closest distance更新:我正在尝试在输入城市的计算列表中找到最小距离:例如:比较 A->B、A->C、A->D,...并选择距离最近的那个

First, change your function to return a numeric value instead of a string (in general you should have functions return values that will let you do other useful things with them in your code, rather than turning them into English-language representations):首先,更改您的 function 以返回数值而不是字符串(通常,您应该有函数返回值,让您可以在代码中使用它们做其他有用的事情,而不是将它们转换为英语表示):

from math import sqrt
def distance(loc1_coordinates, loc2_coordinates):
    point1x, point1y = loc1_coordinates
    point2x, point2y = loc2_coordinates
    return sqrt((point1x-point2x)**2 + (point1y-point2y)**2)  

One of the useful things you can do with this function is to use it as a key in the min function to find the minimum distance:您可以使用此 function 做的一件有用的事情是将其用作min function 中的key以找到最小距离:

def closest_destination(city: str, cities: dict) -> str:
    """Given a city in a {city: coord} dict, return closest other city."""
    other_cities = {k: v for k, v in cities.items() if k != city}
    return min(other_cities, key=lambda o: distance(cities[o], cities[city]))

and then:接着:

cities = {'A':[5,21], 'B':[43,76], 'C':[56,19], 'D':[21,37], 'E':[76,23], 'F':[45,56], 'G':[23,13]}
print(closest_destination('A', cities)) # prints 'G'

A single pass approach is to use the key parameter of min :单通方法是使用min的关键参数:

def closest_destination(source, locations):
    source_location = locations[source]
    return min(locations, key=lambda loc: (loc == source, distance(source_location, locations[loc])))


cities = {'A': [5, 21], 'B': [43, 76], 'C': [56, 19], 'D': [21, 37], 'E': [76, 23], 'F': [45, 56], 'G': [23, 13]}
print(closest_destination('A', cities))  # should be 'G'

Output Output

G

The idea of the function: function的思路:

lambda loc: (loc == source, distance(source_location, locations[loc]))

to map each city key to a tuple where the first value is 1 if it is equal to the source, therefore it will always come last.到 map 每个城市键到一个元组,如果它等于源,则第一个值为 1,因此它将始终排在最后。 Then you break the ties by distance.然后你按距离打破关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM