查找与最接近的数字配对匹配的索引
Find Index That Matches To Closest Provided Number Pairing
我在 Python 中有三个单独的列表,它们都非常大。规定是列表 不能 根据它们当前的显示方式重新排序。每个列表的外观片段如下:
lats = [40.92322342,40.92322342,40.92322342,40.92322342,40.92322342]
lons = [-74.32176109,-74.29518277,-74.26860445,-74.24202613,-74.21544781]
data = [19,19,19,17,18]
我希望提供纬度和经度配对,并希望 return data
列表的索引号和对应值最接近所提供的纬度和经度.
例如,配对 40.9254, -74.2765
将 return 相应的索引号,这将是上面提供的列表片段中的第三组值。
使用 this example,我已经能够按单个列表和 return 相应的索引号对搜索进行分区。但是,索引号不同。
代码:
min(enumerate(lats), key=lambda x: abs(x[1]-40.9254))
min(enumerate(lons), key=lambda x: abs(x[1]-(-74.2765)))
an index #, 40.92322342
a different index # than above, -74.26860445
有什么有效的方法可以解决这个问题吗?
为什么不为纬度和经度创建一个对象。
class LatLong:
def __init__(self, lat, lon):
self.lat = lat
self.lon = lon
然后创建一个对象列表,对于给定的纬度和经度,只需遍历对象列表和return对象
您可以先找到 euclidean distance between two points using sqrt((x[0] - y[0]) ** 2 + (x[1] - y[1]) ** 2)
, then use it combined with min()
作为 key
来找到最近的点。
from math import sqrt
lats = [40.92322342,40.92322342,40.92322342,40.92322342,40.92322342]
lons = [-74.32176109,-74.29518277,-74.26860445,-74.24202613,-74.21544781]
def euclidean_distance(x, y):
return sqrt((x[0] - y[0]) ** 2 + (x[1] - y[1]) ** 2)
def find_closest_point(data, point):
# create (point, index) pairs
indices = ((e, i) for i, e in enumerate(data))
# find smallest point, and only return the index
return min(indices, key=lambda p: euclidean_distance(p[0], point))[1]
print(find_closest_point(zip(lats, lons), (40.9254, -74.2765)))
其中returns第三对坐标(索引从0开始):
2
注意:您可以在元组列表中包含 lats
和 lons
,这样您就不需要调用 zip()
在函数中。
我在 Python 中有三个单独的列表,它们都非常大。规定是列表 不能 根据它们当前的显示方式重新排序。每个列表的外观片段如下:
lats = [40.92322342,40.92322342,40.92322342,40.92322342,40.92322342]
lons = [-74.32176109,-74.29518277,-74.26860445,-74.24202613,-74.21544781]
data = [19,19,19,17,18]
我希望提供纬度和经度配对,并希望 return data
列表的索引号和对应值最接近所提供的纬度和经度.
例如,配对 40.9254, -74.2765
将 return 相应的索引号,这将是上面提供的列表片段中的第三组值。
使用 this example,我已经能够按单个列表和 return 相应的索引号对搜索进行分区。但是,索引号不同。
代码:
min(enumerate(lats), key=lambda x: abs(x[1]-40.9254))
min(enumerate(lons), key=lambda x: abs(x[1]-(-74.2765)))
an index #, 40.92322342
a different index # than above, -74.26860445
有什么有效的方法可以解决这个问题吗?
为什么不为纬度和经度创建一个对象。
class LatLong:
def __init__(self, lat, lon):
self.lat = lat
self.lon = lon
然后创建一个对象列表,对于给定的纬度和经度,只需遍历对象列表和return对象
您可以先找到 euclidean distance between two points using sqrt((x[0] - y[0]) ** 2 + (x[1] - y[1]) ** 2)
, then use it combined with min()
作为 key
来找到最近的点。
from math import sqrt
lats = [40.92322342,40.92322342,40.92322342,40.92322342,40.92322342]
lons = [-74.32176109,-74.29518277,-74.26860445,-74.24202613,-74.21544781]
def euclidean_distance(x, y):
return sqrt((x[0] - y[0]) ** 2 + (x[1] - y[1]) ** 2)
def find_closest_point(data, point):
# create (point, index) pairs
indices = ((e, i) for i, e in enumerate(data))
# find smallest point, and only return the index
return min(indices, key=lambda p: euclidean_distance(p[0], point))[1]
print(find_closest_point(zip(lats, lons), (40.9254, -74.2765)))
其中returns第三对坐标(索引从0开始):
2
注意:您可以在元组列表中包含 lats
和 lons
,这样您就不需要调用 zip()
在函数中。