从元组列表中获取最接近给定值的元组

Jot*_*ani 1 python tuples list coordinates

给定一个包含坐标的元组列表,我想找到哪个坐标最接近我在输入中给出的坐标:

cooList = [(11.6702634, 72.313323), (31.67342698, 78.465323)]
coordinate = (11.6702698, 78.113323)
takenearest(myList, myNumber)
...
(11.6702634, 72.313323)
Run Code Online (Sandbox Code Playgroud)

请告诉我...

eum*_*iro 5

对于您的数据

cooList = [(11.6702634, 72.313323), (31.67342698, 78.465323)]
coordinate = (11.6702698, 78.113323)
Run Code Online (Sandbox Code Playgroud)

最短的Pythonic答案是:

nearest = min(cooList, key=lambda x: distance(x, coordinate))
Run Code Online (Sandbox Code Playgroud)

使用函数distance(a, b)返回点之间的距离a并b作为浮点数,您必须自己定义.

现在你必须决定如何计算距离:使用简单的a² + b² = c²,一些地理公式或专用库.

  • @Jothimani你必须自己实现它. (4认同)
  • @Jothimani请不要告诉我你不能google`python points distance` (3认同)