I am writing a small program and increasing efficiency, I need to find the nearest latitude and longitude in my array.
Suppose you have the following code:
tempDataList = [{'lat': 39.7612992 , 'lon': -86.1519681}, {"lat": 39.762241, "lon": -86.158436}, {"lat": 39.7622292, "lon": -86.1578917}] tempLatList = [] tempLonList = [] for item in tempDataList: tempLatList.append(item['lat']) tempLonList.append(item['lon']) closestLatValue = lambda myvalue: min(tempLatList, key=lambda x: abs(x - myvalue)) closestLonValue = lambda myvalue: min(tempLonList, key=lambda x: abs(x - myvalue)) print(closestLatValue(39.7622290), closestLonValue(-86.1519750))
The result is:
(39.7622292, -86.1519681)
What should be (in this example, the last object in the list)
(39.7622292, -86.1578917)
I know how to get the closest cell with a single value, but I would like to get the lambda function to take both values ββinto account, but I'm not quite sure how to do this. Help?
source share