【发布时间】:2015-10-03 13:09:25
【问题描述】:
我的字典列表中有很多数据点,保存为:
{'加拿大': ['10.625', '80.743'], '圣多美和普林西比': ['32.399', '63.935']}
我需要通过使用公式使用每个数据点中的两个数字来计算它们之间的欧几里得距离:
sqrt{[x_j-x_i]**2 + [y_j-y_i]**2}
我怎样才能用字典做到这一点?我无法弄清楚或找到有关如何使用 {'Canada': ['10.625', '80.743']}
的 [ ] 部分的任何答案我需要编写一个函数来计算字典中两个数据点坐标之间的距离,但我不知道从哪里开始。
def distance():
# compute distance where two data point in a dictionary is used
# data points e.g.: {'Canada': ['10.625', '80.743'], 'Sao Tome and Principe': ['32.399', '63.935']}
distance = result(sqrt{[10.625,32.399]**2 + [80.743,63.935]**2})
【问题讨论】:
标签: python-2.7 dictionary euclidean-distance