在此程序中,我解析json数据,该数据包含python字典的键“ id”和值“ coord”。我是python的新手,很抱歉代码不是pythonic-way。我的问题是在z = myDict.get(n)行中。它总是返回相同的值,但我希望每49次迭代将其下一个键更改一次(因为我确实有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”进行比较:10002、10003、10004等。
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
答案 0 :(得分:-1)
我认为您应该编辑第二个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应该是元组。