python - Append different object into list but the list result in same objects repeating -


im trying create function return neighboring node of state of n-queens on chess board, , below code, generate list of neighboring node based on parameter input state.

#generate neighboring state's function def generate_neighbor_node (cur_state):     current = cur_state     neighbor = current     neighbor_list = []     in range(qs):         chg_value = neighbor[i]         chg_value +=1         if chg_value == qs:             chg_value =0         neighbor[i] = chg_value          print neighbor                neighbor_list.append(neighbor)     #reverse process, change node value original next     #loop generate valid neighbor state         chg_value -=1         if chg_value == -1:            chg_value = (qs-1)         neighbor[i] = chg_value         return neighbor_list                  print board ai = generate_neighbor_node(board) print ai  output this: 1. [1, 3, 2, 0] 2. [2, 3, 2, 0] 3. [1, 0, 2, 0] 4. [1, 3, 3, 0] 5. [1, 3, 2, 1] [[1, 3, 2, 0], [1, 3, 2, 0], [1, 3, 2, 0], [1, 3, 2, 0]] 

but want list contain array 2,3,4 , 5 not 1 how it? please help, thanks.

whenever call neighbor_list.append(neighbor) add reference neighbor, rather new list. means every list in neighbor_list current value of neighbor.

to fix this, should make copy of list this:

neighbor_list.append(copy.copy(neighbor)

documentation copy


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -