Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/331.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_Python 3.x - Fatal编程技术网

Python 计算坐标之间距离的程序

Python 计算坐标之间距离的程序,python,python-3.x,Python,Python 3.x,在这个程序中,我将包含key-'id'和value-'coord'的json数据解析到python字典中。我是python新手,很抱歉代码不是python的方式。我的问题在z=myDict.getn行。它总是返回相同的值,但我希望每49次迭代就更改一个next键,因为我有49个坐标,这是我的json看起来像: {"status" : "200", "result" : [{"id":10001,"coord":"45.781231 78.784321"},{"id":10002,"c

在这个程序中,我将包含key-'id'和value-'coord'的json数据解析到python字典中。我是python新手,很抱歉代码不是python的方式。我的问题在z=myDict.getn行。它总是返回相同的值,但我希望每49次迭代就更改一个next键,因为我有49个坐标,这是我的json看起来像:

 {"status" : "200", 
    "result" : [{"id":10001,"coord":"45.781231 78.784321"},{"id":10002,"coord":"45.481684 78.743410},{"id":10003,"coord":"45.411934 78.543113}, etc the same 50 dicts.
如果你能帮助我,我将非常感激

from math import sin, cos, sqrt, atan2, radians
import requests 
import itertools 


def data():
    response = requests.get('')
    json_data = response.json()
    global myDict
    myDict = {}
    for each in json_data['result']:
    myDict.update ({each['id']: (each['coord'])})


    myDict[10252] = '43.238454 76.949185'
    myDict = {str(k):(v) for k,v in myDict.items()}
    keys, values = zip(*myDict.items())
    for v in itertools.product(*values):
        experiment = dict(zip(keys, v))    
        for key,value in experiment.items():
        x = myDict[key]
        for n,m in experiment.items():
            z = myDict.get(n)
        var1,var2 = x.split()
        var3,var4 = z.split()                                                                                                   
        R = 6373.0 
        lat1 = radians(float(var1)),radians(float(var2))
        lat2 = radians(float(var3)),radians(float(var4))
        dlon = lat2[1]-lat1[1]
        dlat = lat2[0]-lat1[0]
        a = sin(dlat / 2)**2 + cos(lat1[0]) * cos(lat2[0]) * sin(dlon / 2)**2
        c = 2 * atan2(sqrt(a), sqrt(1 - a))
        distance = int((R * c)*1000)
        print ("Result:", distance)
        print(x,z)
 g = data()
 print(g)
我给出的输出是: 正如您所看到的,每49次迭代,数字都是相等的。它将每个id值与唯一的id:10001进行比较。我想把它和id:10002100310004等进行比较

Result: 0
Result: 55
Result: 1987
Result: 1345
Result: 3290
Result: 4380
Result: 2058
Result: 4715
Result: 910
Result: 2501
Result: 1176
Result: 3958
Result: 3622
Result: 534
Result: 1176
Result: 922
Result: 2154
Result: 3440
Result: 2749
Result: 1530
Result: 2139
Result: 919
Result: 1264
Result: 3331
Result: 1559
Result: 3542
Result: 3752
Result: 4684
Result: 3262
Result: 5653
Result: 6239
Result: 5827
Result: 6245
Result: 1444
Result: 754
Result: 1755
Result: 453
Result: 5459
Result: 7287
Result: 1642
Result: 4506
Result: 4266
Result: 493
Result: 1029
Result: 4944
Result: 4744
Result: 5206
Result: 2683
Result: 0
Result: 55
Result: 1987
Result: 1345
Result: 3290
Result: 4380
Result: 2058
Result: 4715
Result: 910
Result: 2501
Result: 1176
Result: 3958
Result: 3622
Result: 534
Result: 1176
Result: 922
Result: 2154
Result: 3440
Result: 2749
Result: 1530
Result: 2139
Result: 919
Result: 1264
Result: 3331
Result: 1559
Result: 3542
Result: 3752
Result: 4684
Result: 3262
Result: 5653
Result: 6239
Result: 5827
Result: 6245
Result: 1444
Result: 754
Result: 1755
Result: 453
Result: 5459
Result: 7287
Result: 1642
Result: 4506
Result: 4266
Result: 493
Result: 1029
Result: 4944
Result: 4744
Result: 5206
Result: 2683
Result: 0
Result: 55
Result: 1987
Result: 1345
Result: 3290
Result: 4380
Result: 2058
Result: 4715
Result: 910
Result: 2501
Result: 1176
Result: 3958
Result: 3622
Result: 534
Result: 1176
Result: 922
Result: 2154
Result: 3440
Result: 2749
Result: 1530
Result: 2139
Result: 919
Result: 1264
Result: 3331
Result: 1559
Result: 3542
Result: 3752
Result: 4684
Result: 3262
Result: 5653
Result: 6239
Result: 5827
Result: 6245
Result: 1444
Result: 754
Result: 1755
Result: 453
Result: 5459
Result: 7287
Result: 1642
Result: 4506
Result: 4266
Result: 493
Result: 1029
Result: 4944
Result: 4744
Result: 5206
Result: 2683

我认为您应该编辑第二个for循环并重新对齐,以便将每个id与其他id进行比较

for v in itertools.product(*values):
        experiment = dict(zip(keys, v))    
        for key,value in experiment.items():
         x = myDict[key]
         z = myDict.get(key)
         var1,var2 = x.split()
         var3,var4 = z.split()                                                                                                   
         R = 6373.0 
         lat1 = radians(float(var1)),radians(float(var2))
         lat2 = radians(float(var3)),radians(float(var4))
         dlon = lat2[1]-lat1[1]
         dlat = lat2[0]-lat1[0]
         a = sin(dlat / 2)**2 + cos(lat1[0]) * cos(lat2[0]) * sin(dlon / 2)**2
         c = 2 * atan2(sqrt(a), sqrt(1 - a))
         distance = int((R * c)*1000)
         print ("Result:", distance)
         print(x,z)
可以使用geopy中的vincenty计算距离

from geopy.distance import vincenty
dist = vincenty(p1, p2)
如果希望结果以公里为单位:

dist = vincenty(p1, p2).kilometers

p1和p2应该是元组

您是否尝试打印n以便查看值的变化?您的程序太复杂了。为什么要构建一个dict,然后将其用作一个列表?并放置提供的示例。哦,我尝试打印,发现问题出在x=myDict[key]中,因为我必须将字符串更改为float。哦,不,问题不在这里。如果您提供预期输出和给定输出,我可以编辑我的答案,使其更有帮助。