我的问题是,在python中将变量附加到列表时,它会保存指向列表中的值的指针,而不是值本身。这意味着,当我更改附加到列表中的变量时,列表中的值也会更改,这是我不想要的。我想在将变量附加到列表中的那一刻保存变量的当前值,并且在附加后该值将不再更改
这是代码,我希望我能说清楚:
while j < n:
if first_graph[j] == (n-1):
while first_graph[j] == (n-1) and j < n-1:
j += 1
if first_graph[j] == (n-1) and j == n-1:
j += 1
else:
first_graph[j] += 1
for i in range(j):
first_graph[i] = 0
all_graphs.append(first_graph)
j = 0
else:
first_graph[j] += 1
all_graphs.append(first_graph)
这显然不是全部,但我的问题是all_graphs
列表中的first_graph
在更改时会被更新,所以最后all_graphs
只是被同一个列表填充了好几次
制作列表的副本:
相关问题 更多 >
编程相关推荐