函数给出错误ValueError:list.remove(x):x不在列表中

我正在编写一个程序,以查找最近的房屋和房屋清单的过程。 这3个函数用于查找课程,最终我打印结果时出现错误:

File "C:/Users/ASUS/.spyder-py3/tp_1.py", line 88, in percurso
  Casas.remove(pos_i)

ValueError: list.remove(x): x not in list
def distancia_casas(P1, P2):
    "returns distance between points"
    delta_x = P2[0] - P1[0]
    delta_y = P2[1] - P1[1]
    distance = ((delta_x ** 2) + (delta_y ** 2)) ** 0.5
    return distance
def casa_mais_proxima(P, Casas):

    pos_inicial = Casas[0]
    pos_mini = (len(Casas), len(Casas))
    dist_mini = distancia_casas(P, pos_mini)

    for c in Casas:
        dist = distancia_casas(P, c)

        if dist < dist_mini:
            pos_mini = c
            dist_mini = dist

        elif dist == dist_mini and pos_inicial[0] > c[0]:
            pos_mini = pos_inicial

        elif dist == dist_mini and pos_inicial[0] < c[0]:
            pos_mini = c

        elif dist == dist_mini and pos_inicial[0] == c[0] and pos_inicial[1] > c[1]:
            pos_mini = pos_inicial

        elif dist == dist_mini and pos_inicial[0] == c[0] and pos_inicial[1] < c[1]:
            pos_mini = c

        elif dist >  dist_mini:
            pos_mini = pos_mini

        else:
            pos_mini = c

    return pos_mini
def percurso(Inicial, Casas):

    pos_i = Inicial

    course = [pos_i,]

    n = len(Casas) - 1

    for c in range(n):
        print(pos_i, Casas)

        pos_i = casa_mais_proxima(pos_i, Casas)

        Casas.remove(pos_i)

        course.append(pos_i)

    if (len(Casas) - 1) != 0:

        course.append(Casas[0])

    return course

print("percurso", percurso((3,2),[(0,1),(1,0),(1,2),(2,3)]))

谁能帮我? 我已经连打了三天,却一无所获

评论