Find the closest match of a list in a list containing lists

后端 未结 2 886
攒了一身酷
攒了一身酷 2021-01-28 15:42

I have a list with two elements like this:

list_a = [27.666521, 85.437447]

and another list like this:

big_list = [[27.666519,          


        
2条回答
  •  情深已故
    2021-01-28 15:58

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

    You can use the key parameter to 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))
    

提交回复
热议问题