从元组列表中,获取最接近给定值的元组 [英] from list of tuples, get tuple closest to a given value
本文介绍了从元组列表中,获取最接近给定值的元组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给出一个包含坐标的元组列表,我想找到哪个坐标最接近我在输入中给出的坐标:
Given a list of tuples containing coordinates, I want to find which coordinate is the closest to a coordinate I give in input:
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)
最短的Python答案是:
the shortest Pythonic answer is:
nearest = min(cooList, key=lambda x: distance(x, coordinate))
带有函数distance(a, b)
,将点a
和b
之间的距离作为浮点数返回,您必须自己定义.
with a function distance(a, b)
returning the distance between the points a
and b
as a float, which you have to define yourself.
现在,您必须决定如何计算距离:使用简单的a² + b² = c²
,某些地理公式或专用的库.
Now you have to decide how you calculate the distance: using simple a² + b² = c²
, some geographical formula or a dedicated library.
这篇关于从元组列表中,获取最接近给定值的元组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文