【发布时间】:2020-04-11 19:43:32
【问题描述】:
我正在尝试计算两个列表之间的距离,以便找到所有坐标之间的最短距离。
这是我的代码:
import random
import math
import copy
def calculate_distance(starting_x, starting_y, destination_x, destination_y):
distance = math.hypot(destination_x - starting_x, destination_y - starting_y) # calculates Euclidean distance (straight-line) distance between two points
return distance
def nearest_neighbour_algorithm(selected_map):
temp_map = copy.deepcopy(selected_map)
optermised_map = [] # we setup an empty optimised list to fill up
# get last element of temp_map to set as starting point, also removes it from temp_list
optermised_map.append(temp_map.pop()) # we set the first element of the temp_map and put it in optimised_map as the starting point and remove this element from the temp_map
for x in range(len(temp_map)):
nearest_value = 1000
neares_index = 0
for i in range(len(temp_map[x])):
current_value = calculate_distance(*optermised_map[x], *temp_map[x])
我在这部分得到一个错误,我不知道为什么:
for i in range(len(temp_map[x])):
current_value = calculate_distance(*optermised_map[x], *temp_map[x])
我试图找出这两个列表之间的点之间的距离,我得到的错误是我的列表索引超出了 for 循环所在的范围
【问题讨论】:
-
你没有告诉我们错误是什么,我不知道为什么。
标签: python list artificial-intelligence nearest-neighbor