Python RecursionError:获取元组的repr时超出了最大递归深度

时间:2017-04-20 20:15:05

标签: python recursion

我已经尝试过针对类似问题的其他一些解决方案,但我仍然遇到问题。这是我的代码:

def nearest_neighbor(cities_tups, temp_dist, best_tour):
   if best_tour == []:
       best_tour.insert(0, cities_tups[0])
       print(best_tour)
       nearest_neighbor(cities_tups, temp_dist, best_tour)
   else:
        last_point = best_tour[len(best_tour) - 1]
        best_next = (math.inf, math.inf)
        for l in cities_tups:
            print('Last Point was:', last_point)
            print('This Point is:', l)
            if((distance((last_point[0],last_point[1]), (l[0],l[1]))) < (distance((last_point[0],last_point[1]), (best_next[0],best_next[1])))):
                print('new best detected')
                distance_to_add = distance((last_point[0],last_point[1]), (l[0],l[1]))
                best_next = l
        best_tour.insert(len(best_tour),best_next)
        temp_dist += distance_to_add
        print('best tour:', best_tour)
        print('temp dist:', temp_dist)
        nearest_neighbor(cities_tups, temp_dist, best_tour)

我正在尝试使用最近邻算法来找到点之间的“最佳”路径。这些点存储在'cities_tups'中,这是一个包含带有x和y坐标的元组的元组。在抛出RecursionError之前,代码将运行几次迭代。我只是耗尽内存还是有办法解决这个问题?

0 个答案:

没有答案