从列表中选择最接近的对 [英] Select the closest pair from a list
本文介绍了从列表中选择最接近的对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个列表,请举个例子
I have a list(s),take an example
[(35.9879845760485, -4.74093235801354), (35.9888687992442, -4.72708076713794),
(35.9889733432982, -4.72758983150694), (35.9915751019521, -4.72772881198689),
(35.9935223025608, -4.72814213543564), (35.9941433944962, -4.72867416528065),
(35.9946670576458, -4.72915181755908), (35.995946587966, -4.73005565674077),
(35.9961479762973, -4.7306870912609), (35.9963563641681, -4.7313535758683),
(35.9968685892892, -4.73182757975504), (35.9976738530666, -4.73194429867996) ]
和coord = (35.9945570576458, -4.73110757975504)
我想从list
推荐答案
编写距离函数,并使用带有键参数的内置min
函数.
Write a distance function and use builtin min
function with key parameter.
>>> from functools import partial
>>> dist=lambda s,d: (s[0]-d[0])**2+(s[1]-d[1])**2 #a little function which calculates the distance between two coordinates
>>> a=[(35.9879845760485, -4.74093235801354), (35.9888687992442, -4.72708076713794), ..... ]
>>> coord = (35.9945570576458, -4.73110757975504)
>>> min(a, key=partial(dist, coord)) #find the min value using the distance function with coord parameter
(35.9961479762973, -4.7306870912609)
这篇关于从列表中选择最接近的对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文