Write a function called dist that takes in two points (so two lists of two elements each), and computes the distance between them. Make sure this works on the example below before proceeding to the next step.
Use dist in a nested loop inside shortestDist to compare each element of the list of points with every element in the list after it. So, basically, find the shortest distance between points in a list.
以下是我目前掌握的情况:sample= [[45, -99], [24, 83], [-48, -68], [-97, 99], [-8, -77], [-2, 50], [44, 41], [-48, -58], [-1, 53], [14, 86], [31, 94], [12, -91], [33, 50], [82, 72], [83, -90], [10, 78], [7, -22], [90, -88], [-21, 5], [6, 23]]
def dist(p0, p1):
return (((p0[0] - p1[0])**2) + ((p0[1] - p1[1])**2))**.5
def shortestDist(sample):
distances = []
for i in range(len(sample)-1):
for j in range(i+1, len(sample)):
distances += [dist(sample[i],sample[j])]
return(min(distances))
找到两点之间的距离。我只需要一些帮助,找出如何开始写shortestDist来比较所有点并跟踪最短距离。更新:错误已解决,我现在可以走了。谢谢大家的帮助!