简体   繁体   English

求解测量点列表 (x, y) 中点之间距离的函数

[英]Solving a function that measures distance between points in a list of points (x, y)

I am writing a function route.我正在写一个函数路由。 This function has a mandatory parameter points that takes a list of points.这个函数有一个强制参数points,它接受一个点列表。 The function must return the total distance traveled if each of the points in the given list is visited in turn.如果依次访问给定列表中的每个点,则该函数必须返回行进的总距离。 Apart from the mandatory parameter, the function also has two optional parameters:除了强制参数外,该函数还有两个可选参数:

cycle: takes a Boolean value that indicates whether the end of the route is equal to its starting point (True) or not (False);循环:取一个布尔值,指示路线的终点是否等于其起点(真)或不等于(假); the default value of this parameter is False此参数的默认值为 False

distance: takes a distance function that is used for the calculation of the total distance between two consecutive points in the given route; distance:采用距离函数,用于计算给定路径中两个连续点之间的总距离; if no explicit value is passed to this parameter, the Euclidean distance must be used如果没有明确的值传递给这个参数,则必须使用欧几里德距离

Problem: Anybody knows with the last definition route() how to solve it for the case:问题:任何人都知道最后一个定义 route() 如何解决这个问题:

route([(41.79, 13.59), (41.68, 14.65), (21.16, -4.79)], distance=lambda p1, p2: abs(p1[0] + p2[0]))

correct answer : 146.31正确答案:146.31

Part of my code I refer to:我引用的部分代码:

 if cycle == False and distance is λ(p1, p2): abs(p1[0] + p2[0]):

            l = list()
            count = 0

            for items in range(len(points)-1):
                a = points[items]
                b = points[items+1]
                d = euclidean(a[0], b[0])
                l.append(d)
                count += 1

            return sum(l)

In this part I got stuck at the first rule and further.在这部分中,我陷入了第一条规则和更远的地方。

Complete code which works fine (except for the part above):工作正常的完整代码(除了上面的部分):

  def euclidean(a, b):
    '''
    >>> euclidean((42.36, 56.78), (125.65, 236.47))
    198.05484139500354
    '''

    from math import sqrt

    return sqrt(sum((a - b)**2 for a, b in zip(a, b)))




def manhattan(c, d):
    '''
    >>> manhattan((42.36, 56.78), (125.65, 236.47))
    262.98
    '''

    return sum(abs(c - d) for c, d in zip(c, d))



def chessboard(e, f):
    '''
    >>> chessboard((42.36, 56.78), (125.65, 236.47))
    179.69
    '''

    return max(abs(e - f) for e, f in zip(e, f))



def route(points, cycle=False, distance=None):
    '''
    >>> route([(6.59, 6.73), (4.59, 5.54), (5.33, -13.98)])
    21.861273201261746
    >>> route(cycle=True, points=[(6.59, 6.73), (4.59, 5.54), (5.33, -13.98)])
    42.60956710702662
    >>> route([(6.59, 6.73), (4.59, 5.54), (5.33, -13.98)], distance=manhattan)
    23.45
    >>> route([(6.59, 6.73), (4.59, 5.54), (5.33, -13.98)], cycle=True, distance=manhattan)
    45.42
    '''



    if cycle == False and distance is None: 

        l = list()
        count = 0

        for items in range(len(points)-1):
            a = points[items]
            b = points[items+1]
            d = euclidean(a, b)
            l.append(d)
            count += 1

        return sum(l)


    if cycle == False and distance is euclidean:

        l = list()
        count = 0

        for items in range(len(points)-1):
            a = points[items]
            b = points[items+1]
            d = euclidean(a, b)
            l.append(d)
            count += 1

        return sum(l)


    if cycle == False and distance is λ(p1, p2): abs(p1[0] + p2[0]):

        l = list()
        count = 0

        for items in range(len(points)-1):
            a = points[items]
            b = points[items+1]
            d = euclidean(a[0], b[0])
            l.append(d)
            count += 1

        return sum(l)



    if cycle == True and distance is None:

        l = list()
        count = 0

        for items in range(len(points)-1):
            a = points[items]
            b = points[items+1]
            d = euclidean(a, b)
            l.append(d)
            count += 1

        f = points[0]
        g = points[-1] 
        r = euclidean(g, f)

        k = sum(l) + r

        return k


    if cycle == True and distance is euclidean:

        l = list()
        count = 0

        for items in range(len(points)-1):
            a = points[items]
            b = points[items+1]
            d = euclidean(a, b)
            l.append(d)
            count += 1

        f = points[0]
        g = points[-1] 
        r = euclidean(g, f)

        k = sum(l) + r

        return k



    if cycle is False and distance is manhattan:

        l = list()
        count = 0

        for items in range(len(points)-1):
            a = points[items]
            b = points[items+1]
            d = manhattan(a, b)
            l.append(d)
            count += 1

        return sum(l)


    if cycle is True and distance is manhattan:

        l = list()
        count = 0

        for items in range(len(points)-1):
            a = points[items]
            b = points[items+1]
            d = manhattan(a, b)
            l.append(d)
            count += 1

        f = points[0]
        g = points[-1] 
        r = manhattan(g, f)

        k = sum(l) + r

        return k

I Agree with Duncan.我同意邓肯。 You have way too much duplication.你有太多的重复。 Here a more direct approach:这里有一个更直接的方法:

euclidean = lambda p1, p2: sqrt(sum((p1_i - p2_i)**2 for p1_i, p2_i in zip(p1, p2)))
manhattan = lambda p1, p2: sum(abs(p1_i - p2_i) for p1_i, p2_i in zip(p1, p2))
chessboard = lambda p1, p2: max(abs(p1_i - p2_i) for p1_i, p2_i in zip(p1, p2))

def route(points, cycle=False, metric=euclidean):
    l = 0.0
    for i in range(len(points) - 1):
        l += metric(points[i], points[i + 1])

    if cycle:
        l += metric(points[-1], points[0])

    return l

Any metric funtion can be passed and is then used instead of the euclidean metric.可以传递任何度量函数,然后使用它来代替欧几里德度量。

暂无
暂无

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

相关问题 在属于行列表的连续点(x,y元组)之间的迭代欧几里德距离计算 - Iterative euclidean distance calculation between consecutive points (x,y tuples) which belongs to a list of lines Python:(x,y)平面中一堆点之间的平均距离 - Python: average distance between a bunch of points in the (x,y) plane 点到点列表之间的距离 - Distance between point to list of points 蟒蛇。 如何从ax,y列表和偏移距离获取偏移样条的x,y坐标 - Python. How to get the x,y coordinates of a offset spline from a x,y list of points and offset distance 在python中使用scipy.spatial.distance.cdist(X,Y)查找一组点之间的距离 - finding the distance between a set of points using scipy.spatial.distance.cdist(X, Y) in python x两条点之间的距离 - x distance between two lines of points 单位球体上两个“P = (x, y, z)”点之间的大圆距离 - Great Circle Distance between two "P = (x, y, z)" points on a unit sphere 如何优化 2 个点 (x,y,z) 和两个 arrays 之间的距离 - How can I optimize the distance between 2 points (x,y,z) and two arrays 计算两点之间的距离,如果 &lt;1 :添加到列表 x 和 y 坐标(人口程序)Python - calculate distance between two points, if <1 : add to lists x and y coords (population program) Python 如何从 dlib 68 x-y 坐标测量面部地标中 2 个点之间的距离 - how to mesure the distance between 2 points in the facial landmarks from dlib 68 x- y-coordinates
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM