全局列表没有任何附加信息

时间:2016-09-20 04:43:42

标签: python list global

answers = []

def search(visit_order, nodes_to_visit, distance):

    if len(nodes_to_visit) == 0:
        print visit_order
        answers.append(visit_order)
        return
    else:
        for node in nodes_to_visit:
            nodes_to_visit.remove(node)
            visit_order.append(node)
            search(visit_order, nodes_to_visit, 0)
            visit_order.remove(node)
            nodes_to_visit.append(node)

search([],nodes, 0)
print answers

我有一个全局列表answers和一个递归函数,该函数遍历给定的nodes_to_visit列表,如果没有visit_order列表,answers列表将nodes_to_visit添加Visit_order 1}}。

当我在追加之前打印answers时,我得到一个正确的值。但是,当我打印[[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []]时,我只会获得#include <iostream> #include <string> using namespace std; void Print(char *arr); int main() { char arr1[] = { "Hello how are you? / " }; Print(arr1); return 0; } void Print(char *arr) { int i = 0; while (arr[i] != '/') { cout << arr[i] << endl; ++i; } } 等列表。有什么问题?

例如,如果我给搜索([],[1,2,3,4],0)作为输入它应该给我一些像 [[3,1,2,4],[3,1,2,4],[3,1,2,4],[3,1,2,4],[3,1,2,4] ,[3,1,2,4],[3,1,2,4],[3,1,2,4],[3,1,2,4],[3,1,2,4] ,[3,1,2,4],[3,1,2,4],[3,1,2,4],[3,1,2,4],[3,1,2,4] ,[3,1,2,4],[3,1,2,4],[3,1,2,4],[3,1,2,4],[3,1,2,4] ,[3,1,2,4],[3,1,2,4],[3,1,2,4],[3,1,2,4]] 但它给了我[[],[],[],[],[],[],[],[],[],[],[],[],[],[],[], [],[],[],[],[],[],[],[],[]]代替。

1 个答案:

答案 0 :(得分:0)

所以问题是你要将相同的对象附加到answers,然后将其清空。检查[id(e) for e in answers]的输出,您应该看到相同的对象ID。快速解决方法是使用answers.append(list(visit_order))answers.append(visit_order[:])

附加副本
In [4]: search([],[1,2,3,4],0)

In [5]: answers
Out[5]: 
[[],
 [],
 [],
 [],
 [],
 [],
 [],
 [],
 [],
 [],
 [],
 [],
 [],
 [],
 [],
 [],
 [],
 [],
 [],
 [],
 [],
 [],
 [],
 []]

In [6]: [id(e) for e in answers]
Out[6]: 
[140399731251400,
 140399731251400,
 140399731251400,
 140399731251400,
 140399731251400,
 140399731251400,
 140399731251400,
 140399731251400,
 140399731251400,
 140399731251400,
 140399731251400,
 140399731251400,
 140399731251400,
 140399731251400,
 140399731251400,
 140399731251400,
 140399731251400,
 140399731251400,
 140399731251400,
 140399731251400,
 140399731251400,
 140399731251400,
 140399731251400,
 140399731251400]

In [7]:

但是,如果我将功能更改为:

def search(visit_order, nodes_to_visit, distance):

    if len(nodes_to_visit) == 0:
        answers.append(visit_order[:])
        return
    else:
        for node in nodes_to_visit:
            nodes_to_visit.remove(node)
            visit_order.append(node)
            search(visit_order, nodes_to_visit, 0)
            visit_order.remove(node)
            nodes_to_visit.append(node)

现在...

In [8]: answers = []

In [9]: search([],[1,2,3,4],0)

In [10]: answers
Out[10]: 
[[1, 2, 3, 4],
 [1, 2, 3, 4],
 [1, 3, 4, 2],
 [1, 3, 4, 2],
 [1, 3, 2, 4],
 [1, 3, 2, 4],
 [2, 4, 3, 1],
 [2, 4, 3, 1],
 [2, 3, 1, 4],
 [2, 3, 1, 4],
 [2, 3, 4, 1],
 [2, 3, 4, 1],
 [3, 1, 4, 2],
 [3, 1, 4, 2],
 [3, 4, 2, 1],
 [3, 4, 2, 1],
 [3, 4, 1, 2],
 [3, 4, 1, 2],
 [3, 2, 1, 4],
 [3, 2, 1, 4],
 [3, 1, 4, 2],
 [3, 1, 4, 2],
 [3, 1, 2, 4],
 [3, 1, 2, 4]]

In [11]: