Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 移动值以查找给定纬度和经度的三个最近点_Python_Dictionary_Key - Fatal编程技术网

Python 移动值以查找给定纬度和经度的三个最近点

Python 移动值以查找给定纬度和经度的三个最近点,python,dictionary,key,Python,Dictionary,Key,我要找到给定经度和纬度的三个最近的点作为输入,其中self.distance是一个函数,用于进行计算。以下是我到目前为止的情况: def closest_stations(self, latitude, longitude): loaded_json = self.status.json() first_closest = 0.00 second_closest = 0.00 third_closest = 0.00 temp = 0.00 tem

我要找到给定经度和纬度的三个最近的点作为输入,其中self.distance是一个函数,用于进行计算。以下是我到目前为止的情况:

def closest_stations(self, latitude, longitude):
    loaded_json = self.status.json()
    first_closest = 0.00
    second_closest = 0.00
    third_closest = 0.00
    temp = 0.00
    temp2 = 0.00
    new_dict = {}
    for row in loaded_json['data']['stations']:
        row_lat = float(row["lat"])
        row_lon = float(row["lon"])
        distance = self.distance(latitude, longitude, row_lat, row_lon)
        temp_name = row["station_id"]
        temp_num = row['name']
        print(distance)
        if distance < first_closest or first_closest == 0.00: #first closest
            temp = first_closest
            temp2 = second_closest
            first_closest = distance
            second_closest = temp
            third_closest = temp2
            row_num1 = row["station_id"]
            row_name1 = row['name']
        elif (first_closest < distance < second_closest) or second_closest == 0.00: #between 1st and 2nd
            third_closest = second_closest
            second_closest = distance
            #row_num2 = row_num1
            #row_name2 = row_name1
            row_num2 = row["station_id"]
            row_name2 = row['name']
        elif (second_closest < distance < third_closest) or third_closest == 0.00: #between 2nd and 3rd
            third_closest = distance
            row_num3 = row["station_id"]
            row_name3 = row['name']
        print(first_closest, second_closest, third_closest)
    new_dict ={row_num1: row_name1, row_num2: row_name2, row_num3: row_name3}
    return new_dict
def最近的_站(自身、纬度、经度):
已加载的_json=self.status.json()
第一个最接近=0.00
第二个最接近=0.00
第三方=0.00
温度=0.00
temp2=0.00
new_dict={}
对于加载的_json['data']['stations']中的行:
行_lat=浮动(行[“lat”])
行长=浮动(行长)
距离=自身距离(纬度、经度、纬度、经度)
临时名称=行[“站点id”]
temp_num=行['name']
打印(距离)
如果距离<第一最近或第一最近==0.00:#第一最近
温度=第一个最接近的温度
temp2=第二个
第一个最近距离=距离
第二个=温度
第三方=临时2
行num1=行[“站id”]
row_name1=row['name']
elif(第一最近距离<距离<第二最近距离)或第二最近距离==0.00:#介于第一和第二之间
第三最近=第二最近
第二个最近距离=距离
#行num2=行num1
#行名称2=行名称1
row_num2=row[“车站id”]
row_name2=row['name']
elif(第二最近距离<距离<第三最近距离)或第三最近距离==0.00:#介于第二和第三之间
第三方=最近距离
row_num3=row[“车站id”]
row_name3=行['name']
打印(第一最近、第二最近、第三最近)
new_dict={row_num1:row_name1,row_num2:row_name2,row_num3:row_name3}
返回新命令
我找到了三个最近距离的逻辑,并且发现我的代码正确地找到了最短距离,如图所示。顶部的数字是计算的距离,三个是被替换的数字(以此类推)。我遇到的问题是,我需要返回一个字典,其中station_id映射到一个站名,我将其用作行['station_id]和行['name']。这是我需要帮助的部分,我想知道是否有人能给我指点。我知道如何随着距离“移动”,但我不知道如何通过字典值本身来实现这一点

例如,假设我们有最短距离1、4和6,并引入了一个新的距离3。在这种情况下,3将取代4,4将向右移动。我知道如何使用距离来实现这一点,从而使3个最短的距离:1、3和4。但是我完全不知道如何使用这本字典