簡體   English   中英

從元組列表中查找最接近的元組 - Python

[英]Find closest tuple from list of tuples - Python

我有包含緯度和經度的元組列表

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)
]

現在我有了另一個坐標:

coordinate = 48.36720362305641, 11.112587917596102

我想知道從上面的列表到給定coordinate的最接近的元組

我試過了:

min(list_of_tuples, key=lambda c: (point[0]- coordinate[0])**2 + (point[1]-coordinate[1])**2)

使用上面的代碼時,我總是從列表中獲得第一個元組。

您沒有在 lambda function 中使用局部變量,而是引用了在其他地方定義的名為point的變量。

min(list_of_tuples, key=lambda point: (point[0] - coordinate[0])**2 + (point[1] - coordinate[1])**2)

我將c重命名為point ,所以您的 function 仍然有效並且很冗長。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM