问题来自state
变量(在 args 中)。它在我的代码中进行了修改(new_state
修改后)。但是,我读过 usinglist()
可以防止这种问题(它看起来state
和new_state
具有相同的参考)。
总而言之,如果我在函数的开头显示 state 的值,而在 return 之前,值是不同的(我显然不想改变这个变量的值!)。我怎么解决这个问题 ?
def successor(self, state, numberClients, numberDepots, Q, dist_table):
succ_list = list()
for i in range(0, len(state)):
for j in range(0, len(state[i])):
switchIndex = 0
while switchIndex < length:
permutationIndex = 0
while permutationIndex < len(state[switchIndex]):
new_state = list(state)
temp = new_state[switchIndex][permutationIndex]
new_state[switchIndex][permutationIndex] = new_state[i][j]
new_state[i][j] = temp
if checkConst(new_state): # accept only in some cases (we don't care here)
succ_list.append(('act', new_state))
permutationIndex += 1
switchIndex += 1
return succ_list