이 문제는 state
변수 (args)에서 발생합니다. 내 코드에서 수정되었습니다 (new_state
수정 이후). 그러나, 나는 list()
을 사용하여 이런 종류의 문제를 막을 수 있다고 읽었습니다 (state
과 new_state
은 같은 참조 번호를가집니다).Python에서 참조 오류가 발생했습니다.
요약하면, 함수의 시작 부분과 반환 바로 앞의 state 값을 표시하면 값이 달라집니다. (분명히이 변수의 값을 변경하고 싶지 않습니다!). 이 문제를 어떻게 해결할 수 있습니까?
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
확인 deepcopy 트릭, 고마워 할 것입니다! 예 목록 목록입니다. – Scipion
차가움. 답변을 수락하고 계속 진행하십시오. – GodMan