我正在尝试使用它们的坐标来计算两个位置之间的距离。但是我不知道如何访问坐标值,因为它们在字典中。
我对编码非常陌生,并且不了解有关此问题的任何代码,因为它对我来说太高级了。我真的不知道从哪里开始。我的主要功能是创建字典:(编辑)
def main():
filename = input("Enter the filename:\n")
file= open(filename,'r')
rows= file.readlines()
d = {}
list = []
for x in rows:
list.append(x)
#print(list)
for elem in list:
row = elem.split(";")
d[row[3]] = {row[0],row[1]} #these are the indexes that the name and latitude & longitude have in the file
{'Location1':{'40 .155444793742276','28 .950292890004903'},'Location2':...}
字典就是这样,所以键是名称,然后坐标是值。这是该函数,到目前为止几乎不包含任何内容:
def calculate_distance(dictionary,location1,location2):
distance_x = dictionary[location1] - dictionary[location2]
# Here I don't know how I can get the values from the dictionary,# since there are two values,longitude and latitude...
distance_y = ...
distance = ... # Here I will use the pythagorean theorem
return distance
基本上,我只需要知道如何使用字典,因为我不知道如何获取这些值,因此可以使用它们来计算距离。 ->如何从字典中搜索键并获取值供我使用。感谢您回答我的愚蠢问题。 :)