簡體   English   中英

求解測量點列表 (x, y) 中點之間距離的函數

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

我正在寫一個函數路由。 這個函數有一個強制參數points,它接受一個點列表。 如果依次訪問給定列表中的每個點,則該函數必須返回行進的總距離。 除了強制參數外,該函數還有兩個可選參數:

循環:取一個布爾值,指示路線的終點是否等於其起點(真)或不等於(假); 此參數的默認值為 False

distance:采用距離函數,用於計算給定路徑中兩個連續點之間的總距離; 如果沒有明確的值傳遞給這個參數,則必須使用歐幾里德距離

問題:任何人都知道最后一個定義 route() 如何解決這個問題:

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

正確答案:146.31

我引用的部分代碼:

 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)

在這部分中,我陷入了第一條規則和更遠的地方。

工作正常的完整代碼(除了上面的部分):

  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

我同意鄧肯。 你有太多的重復。 這里有一個更直接的方法:

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

可以傳遞任何度量函數,然后使用它來代替歐幾里德度量。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM