在包含列表的列表中查找与列表最接近的匹配项 [英] Find the closest match of a list in a list containing lists

查看:38
本文介绍了在包含列表的列表中查找与列表最接近的匹配项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含两个元素的列表,如下所示:

I have a list with two elements like this:

list_a = [27.666521, 85.437447]

还有一个像这样的列表:

and another list like this:

big_list = [[27.666519, 85.437477], [27.666460, 85.437622], ...]

我想在 list_b 中找到与 list_a 最接近的匹配.

And I want to find the closest match of list_a within list_b.

例如,这里最接近的匹配是 [27.666519, 85.437477].

For example, here the closest match would be [27.666519, 85.437477].

我如何才能实现这一目标?

How would I be able to achieve this?

我在此处发现了一个类似的问题,用于寻找最接近的匹配数组中的字符串,但对于上述问题无法类似地重现它.

I found a similar problem here for finding the closest match of a string in an array but was unable to reproduce it similarly for the above mentioned problem.

P.S.列表中的元素是地球上点的坐标.

P.S.The elements in the list are the co-ordinates of points on the earth.

推荐答案

从你的问题来看,很难说你想如何测量距离,所以我只是假设你的意思是欧几里得距离.

From your question, it's hard to tell how you want to measure the distance, so I simply assume you mean Euclidean distance.

您可以将key参数用于min():

from functools import partial

def distance_squared(x, y):
    return (x[0] - y[0])**2 + (x[1] - y[1])**2

print min(big_list, key=partial(distance_squared, list_a))

这篇关于在包含列表的列表中查找与列表最接近的匹配项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆