给定一个包含坐标的元组列表,我想找到哪个坐标最接近我在输入中给出的坐标:
cooList = [(11.6702634, 72.313323), (31.67342698, 78.465323)]
coordinate = (11.6702698, 78.113323)
takenearest(myList, myNumber)
...
(11.6702634, 72.313323)
请告诉我...
最佳答案
为您的数据
cooList = [(11.6702634, 72.313323), (31.67342698, 78.465323)]
coordinate = (11.6702698, 78.113323)
最短的 Pythonic 答案是:
nearest = min(cooList, key=lambda x: distance(x, coordinate))
使用函数 distance(a, b)
返回点 a
和 b
之间的距离作为 float ,你必须定义你自己。
现在您必须决定如何计算距离:使用简单的 a² + b² = c²
、一些地理公式或专用库。
关于python - 从元组列表中,获取最接近给定值的元组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28339199/