简体   繁体   English

如何遍历城市列表以计算它们之间的距离

[英]How to loop through list of cities to calculate distances between them

I'm working on a brain teaser where I want to calculate all of the possible distances between 4 cities.我正在开发一个脑筋急转弯,我想计算 4 个城市之间所有可能的距离。 I wrote a function where you can input the x and y coordinates of the two cities and it'll calculate the distance between them.我写了一个函数,你可以输入两个城市的 x 和 y 坐标,它会计算它们之间的距离。

While I can individually call the function 6 times, that seems inefficient if the data set gets bigger.虽然我可以单独调用该函数 6 次,但如果数据集变大,这似乎效率低下。 I think I should be using nested "for loops" but I can't figure out a way to properly increment the inner loop.我想我应该使用嵌套的“for 循环”,但我想不出正确增加内循环的方法。

My initial idea was to create a list of object and use that in inner loop.我最初的想法是创建一个对象列表并在内部循环中使用它。

 import math #Imports the math module def calc_euclidean(x1,y1,x2,y2): #Function takes 4 arguments xDistancesqrd=math.pow((x2-x1),2) #x2-x1 squared yDistancesqrd=math.pow((y2-y1),2) #y2-y1 squared euclideanDistance=math.sqrt(xDistancesqrd+yDistancesqrd) #distance=square root (x2-x1)^2+(y2-y1)^2 return euclideanDistance #Returns the result of the calculation, the euclidean distance between the points. Budapest=[47.4979, 19.0402] Vienna=[48.210033, 16.363449] Sofia=[42.6977, 23.3219] Zagreb=[45.8150, 15.9819] cities=[Budapest,Vienna,Sofia,Zagreb]

Use itertools.combinations() like:使用itertools.combinations()像:

Code:代码:

for c1, c2 in it.combinations(cities, 2):
    print(c1, c2, calc_euclidean(c1[0], c1[1], c2[0], c2[1]))

Test Code:测试代码:

import math  # Imports the math module
import itertools as it


def calc_euclidean(x1, y1, x2, y2):  # Function takes 4 arguments
    xDistancesqrd = math.pow((x2 - x1), 2)  # x2-x1 squared
    yDistancesqrd = math.pow((y2 - y1), 2)  # y2-y1 squared
    euclideanDistance = math.sqrt(
        xDistancesqrd + yDistancesqrd)  # distance=square root (x2-x1)^2+(y2-y1)^2
    return euclideanDistance  # Returns the result of the calculation, the euclidean distance between the points.


Budapest = [47.4979, 19.0402]
Vienna = [48.210033, 16.363449]
Sofia = [42.6977, 23.3219]
Zagreb = [45.8150, 15.9819]

cities = [Budapest, Vienna, Sofia, Zagreb]
for c1, c2 in it.combinations(cities, 2):
    print(c1, c2, calc_euclidean(c1[0], c1[1], c2[0], c2[1]))

Results:结果:

[47.4979, 19.0402] [48.210033, 16.363449] 2.769860885620431
[47.4979, 19.0402] [42.6977, 23.3219] 6.432330443159777
[47.4979, 19.0402] [45.815, 15.9819] 3.4907522541710128
[48.210033, 16.363449] [42.6977, 23.3219] 8.877266213327731
[48.210033, 16.363449] [45.815, 15.9819] 2.4252345681376934
[42.6977, 23.3219] [45.815, 15.9819] 7.974531916670721

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

相关问题 如何遍历坐标列表并在 Python 中计算它们之间的距离 - How to iterate through a list of coordinates and calculate distance between them in Python 如何遍历坐标列表并计算它们之间的距离? - How to iterate through a list of coordinates and calculate distance between them? Python - 如何加快城市之间距离的计算 - Python - how to speed up calculation of distances between cities 如何根据链 B 上的原子列表计算链 A (CZ) 原子的受体特定原子之间的距离 - How to calculate distances between receptor particular atom of chain A (CZ) atoms against a list of atoms on chain B 如何查找列表中元组之间的距离 - How to find the distances between tuples in a list 如何计算所有数据点之间的距离 - how to calculate the distances between all datapoints among each other 如何使用function来计算两个城市之间的距离 - how to use a function to calculate the distance between two cities 使用单个 for 循环的列表中相同数字之间的距离 - Distances between same numbers in list using single for loop 如何遍历值列表并将它们复制并粘贴到 PyAutoGui 中? - How to loop through a list of values and copy and paste them in PyAutoGui? 通过列表循环引用它们时如何修改 dataframe - how to modify dataframe when referring them through a list loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM