[英]Find closest tuple from list of tuples - Python
I have list of tuples which contains latitudes and longitudes我有包含纬度和经度的元组列表
list_of_tuples = [
(48.383015375128444, 11.112193974456085),
(48.38301264157321, 11.112228110565772),
(48.383007738644274, 11.112256631732311),
(48.38300037666574, 11.112279943681244),
(48.382990265960615, 11.11229845213883),
(48.38297711685105, 11.112312562832122),
(48.382960639658386, 11.112322681489001),
(48.38294054470342, 11.112329213838153),
(48.38291654230646, 11.112332565609025)
]
Now I have another coordinate:现在我有了另一个坐标:
coordinate = 48.36720362305641, 11.112587917596102
and I want to know which is the closest tuple from above list to given coordinate
我想知道从上面的列表到给定
coordinate
的最接近的元组
I tried:我试过了:
min(list_of_tuples, key=lambda c: (point[0]- coordinate[0])**2 + (point[1]-coordinate[1])**2)
When using above code, I am always getting 1st tuple from the list.使用上面的代码时,我总是从列表中获得第一个元组。
You're not using your local variable in your lambda function, instead you reference a variable called point
that is defined somewhere else.您没有在 lambda function 中使用局部变量,而是引用了在其他地方定义的名为
point
的变量。
min(list_of_tuples, key=lambda point: (point[0] - coordinate[0])**2 + (point[1] - coordinate[1])**2)
I renamend c
to point
, so your function still works and is verbose.我将
c
重命名为point
,所以您的 function 仍然有效并且很冗长。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.