列表仅存储最后一次迭代

时间:2019-07-09 22:54:26

标签: python dataframe

该代码的最后一次迭代被存储在数据帧“ df1”中。我正在尝试创建一个列表,该列表连续显示每次迭代的结果。例如:第1行的第1次迭代,第2行的第2次迭代,等等...

我已经放弃将变量的计算包括在内,因为它与问题无关。任何想法如何最好地解决这个问题?

我试图更改缩进并在当前的“ all_possible_path中的for路径”循环中使用for循环。

for i in range(4):
    count = range(i)

 shortest_path= None
    shortest_path_length = None
    for path in all_possible_path:
        fullpath = u + s + d
        euclidean_length = total_distance(fullpath)
        print("Path: {} has total length:{}".format(fullpath, euclidean_length))
        df1 = [fullpath, euclidean_length]


        if shortest_path_length is None or shortest_path_length > euclidean_length:
            shortest_path_length = euclidean_length
            shortest_path = fullpath

我没有收到任何错误消息,但是我的设置似乎有问题。感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

使用以下代码替换您的代码。如果我正确理解了您的问题,它应该可以工作。

for i in range(4):
    count = range(i)

 shortest_path= None
    shortest_path_length = None
    for path in all_possible_path:
        fullpath = u + s + d
        euclidean_length = total_distance(fullpath)
        print("Path: {} has total length:{}".format(fullpath, euclidean_length))
        df1 = df1.append([fullpath, euclidean_length]) // this is list of lists so it should keep data from each iteration.

        if shortest_path_length is None or shortest_path_length > euclidean_length:
            shortest_path_length = euclidean_length
            shortest_path = fullpath

您的数据现在存储在df1中。请检查并告知我们。