【发布时间】:2019-11-06 15:30:47
【问题描述】:
我正在尝试使用它们的坐标计算两个位置之间的距离。但是我不知道如何访问坐标值,因为它们在字典中。
我对编码非常陌生,并且不理解我发现的有关此问题的任何代码,因为它对我来说太高级了。我真的不知道从哪里开始。我的主要功能创建字典:(编辑)
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
基本上我只需要知道如何使用字典,因为我不知道如何获取这些值,因此我可以使用它们来计算距离。 --> 如何从字典中搜索一个键并获取我使用的值。谢谢你回答我的愚蠢问题。 :)
【问题讨论】:
-
This one 可能会有所帮助。
-
您能否给我们a)创建dict的主要功能部分和b)type(dictionary [location1])的值?
-
欧几里得距离在纬度/经度坐标上不是一个好主意:例如点
(0, 90)和(180, 90)在球体上完全相同的位置,但欧几里得距离返回180。请改用半正弦公式。
标签: python dictionary