gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 14:27:24 26 4
gpt4 key购买 nike

我正在编写一个函数路由。该函数有一个强制参数点,它采用点列表。如果依次访问给定列表中的每个点,该函数必须返回行驶的总距离。除了强制参数之外,该函数还有两个可选参数:

cycle:采用 bool 值,指示路线的终点是否等于其起点(True)或不等于(False);该参数默认值为False

距离:采用距离函数,用于计算给定 route 两个连续点之间的总距离;如果没有明确的值传递给该参数,则必须使用欧氏距离

问题:任何人都知道最后一个定义的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

可以传递任何度量函数,然后使用它来代替欧几里德度量。

关于python - 求解测量点列表 (x, y) 中点之间距离的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47534660/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com