简体   繁体   English

Python最短距离点

[英]Python shortest distance points

Write a function called dist that takes in two points (so two lists of two elements each), and computes the distance between them. 编写一个名为dist的函数,该函数接收两个点(每个点包含两个元素的两个列表),并计算它们之间的距离。 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. 使用dist里面嵌套循环shortestDist以点列表中的每个元素在它之后列表中的每个元素进行比较。 So, basically, find the shortest distance between points in a list. 因此,基本上,找到列表中点之间的最短距离。

Here's what I have so far: 这是我到目前为止的内容:

        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))

That finds the distance alright between two points. 这样就可以找到两点之间的距离。 I just need some help figuring out how to start writing shortestDist to compare all points and keep track of the shortest distance. 我只需要一些帮助找出如何开始编写shortestDist来比较所有点并跟踪最短距离。 UPDATE: The error was resolved and I'm good to go now. 更新:该错误已解决,我很高兴现在就去。 Thank you to everyone for their assistance! 谢谢大家的帮助!

This should do the job 这应该做的工作

def shortestDist(points):
        sh = float("inf")
        for i in range(1, len(points)):
                d = dist(points[i-1], points[i])
                if d < sh:
                        sh = d
        return sh

This is a fully functioning example based on a list of points. 这是一个基于点列表的功能全面的示例。

points = [(1,5), (5,8), (8,1), (9,5)]

def euclideanDistance(coordinate1, coordinate2):
    return pow(pow(coordinate1[0] - coordinate2[0], 2) + pow(coordinate1[1] - coordinate2[1], 2), .5)

distances = []
for i in range(len(points)-1):
    for j in range(i+1, len(points)):
        distances += [euclideanDistance(points[i],points[j])]
print min(distances)

I think code below would move you into right direction: 我认为以下代码可以将您带向正确的方向:

def shortestDist(points):
    min_dist = None
    while points:
        p1, p2 = points.pop()
        dist = dist(p1, p2)
        if min_dist is None:
            min_dist = dist
        else:
            min_dist = dist  if dist < min_dist else min_dist

Let me know if you don't understand some parts of code and i will give more explanations to it. 如果您不理解某些代码部分,请告诉我,我将对其进行更多说明。

Good luck! 祝好运!

First, you might want to use tuples rather than lists. 首先,您可能要使用元组而不是列表。 Either way will work, but given the values "x" and "y" are different, albeit both "numbers" (doubles,int...), a tuple is typically used. 两种方法都可以,但是给定值“ x”和“ y”不同,尽管两个“数字”(doubles,int ...)都通常使用元组。

You can pass in the points like: 您可以传递以下要点:

dist((0,1), (2,3))

And they can be accessed like you would access a list: 可以像访问列表一样访问它们:

p0[0] # access "x" in point 0
p0[1] # access "y" in point 0

As for writing shortestDistance , you'll want to take in a list of the tuples from above: eg [(0,1),(2,4),(3,2),(1,3)] 至于写入shortestDistance ,您将要从上方获取一个元组列表:例如[[0,1),(2,4),(3,2),(1,3)]

So something like this for the def: 所以这样的定义:

def shortestDist(listOfPoints)

Then for each point you can compare it to each point after it using the following, which stores it in a dictionary. 然后,您可以使用以下方法将每个点与之后的每个点进行比较,并将其存储在字典中。

 currentIndex = 0
 pointDict = {}
 for point in listOfPoints:
    currentPoint = point
    for i in range(currentIndex,len(listOfPoints)):
        pointDict[currentPoint] = dist(currentPoint,listOfPoints[i])

That should get you started. 那应该让您开始。 It assumes that the points are not duplicated. 假定这些点不重复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM