简体   繁体   English

计算点之间的总距离

[英]Calculating total distance between points

I wrote this function which finds the distance between two points.我写了这个 function 找到两点之间的距离。 Now my next task is to write a function that takes in a list of points which are tuples and calculates the total distance by using the distance function to find the distance of each pair of points and I am stuck I don't know how to go about it any help please?现在我的下一个任务是编写一个 function,它接受一个点列表,这些点是元组,并通过使用距离 function 计算总距离来找到每对点的距离,我被卡住了我不知道如何 go关于它有什么帮助吗? This is the first function:这是第一个 function:

def distance(point1, point2):
    dist = math.sqrt((point2[0] - point1[0])** 2 + (point2[1] - point1[1])** 2) 
    return dist

Assuming that you have an even list and each pair you want to calculate:假设您有一个偶数列表和您要计算的每一对:

list_points = [(2,1),(50,1), (30,1), (30,5)]
for i in range(0,len(list_points),2):
    pair = list_points[i:i+2]
    dist(pair[0], pair[1])

The third parameter in range is a way to batch. range 中的第三个参数是一种批处理方式。 Then if you want to make it a function:那么如果你想让它成为 function:

def compute_distance(list_points):
    """compute distance of points give a list of tuples"""
    computed_list = []
    for i in range(0,len(list_points),2):
        pair = list_points[i:i+2]
        computed_list.append(dist(pair[0], pair[1]))
    return computed_list

Try this.尝试这个。

import math

def calc_total_distance(point_list):
    total_dist = 0
    for i in range(len(point_list)-1):
        dist = math.sqrt((point_list[i][0] - point_list[i+1][0])** 2 + (point_list[i][1] - point_list[i+1][1])** 2)
        total_dist += dist
    return(total_dist)

point_list = [(1,2), (2,2), (2,10)]
print(calc_total_distance(point_list))

From what I understand, your distance function takes in two tuples as argument.据我了解,您的距离 function 以两个元组作为参数。 So for the new function you'd have a list containing nested tuples of the sort:因此,对于新的 function,您将有一个包含此类嵌套元组的列表:

list_points = [((2, 1), (50, 1)), ((30, 1), (30, 5))]

Note that each tuple contains two tuples.请注意,每个元组包含两个元组。

If this is the case, here's how I'll write the new function:如果是这种情况,下面是我将如何编写新的 function:

    def total_distance(list_distances):
        result = [distance(i[0], i[1]) for i in list_distances]
        return sum(result)

The second line here uses list comprehension to loop through the list and map the items to your distance function. The sum of all the distances obtained is returned这里的第二行使用列表理解循环遍历列表和 map 项目到你的距离 function。返回所有获得的距离之和

You need to sum() the distanced between each pair of points.您需要对每对点之间的距离sum() A good way to get the pairs is by using a generator function like the one named pairwise() shown below.获得配对的一种好方法是使用生成器 function ,如下所示的名为 pairwise pairwise()的生成器。 You're also reinventing the wheel with regards to calculating the distance between two points — because there is a math.hypot() function.关于计算两点之间的距离,您也在重新发明轮子 — 因为有一个math.hypot() function。

The code below shows using them together to do what you want:下面的代码显示了一起使用它们来做你想做的事情:

import math

def pairwise(iterable):
    """s -> (s0,s1), (s1,s2), (s2, s3), ..."""
    a, b = iter(iterable), iter(iterable)
    next(b, None)
    return zip(a, b)

def distance(point1, point2):
    return math.hypot(point2[0]-point1[0], point2[1]-point1[1])


if __name__ == '__main__':
    points = [(1,2), (2,2), (2,10)]
    print(f'total distance:', sum(distance(a, b) for a,b in pairwise(points)))

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

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