0

so i have this project for school that I'm making and in it we need to make a change to a list named board. We also have to return the changes that we have made in form of a sequence. I defind "board" as a list of tuples and sets and made a function that changes a specific set in "board", then i made another function that looks like this:

def function (board,pos):
#pos is a tuple (x,y)
    begin_open_positions = board[2]

    disclose_help(board,pos)
    #this function changes the board

    end_open_positions = board[2]
    added_pos = begin_open_positions-end_open_positions
    return added_pos
#board at the start = [(4, 4), [(0, 0)], set(), ((0, 0), (0, 1), (0, 1))]
#board at the end =[(4, 4), [(0, 0)], {(1, 2), ... ,(1, 1)}, ((0, 0),...)]

the question is why does my begin_open_position change and how can i make it so it doesnt change and stays (in this case) set().

the disclose help function just adds positions to board[2]

edit: i tried to use copy.copy(x) didnt work

  • `begin_open_positions` and `end_open_positions` are just alternate names for the object at `board[0]`. Please see [Other languages have "variables", Python has "names"](http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#other-languages-have-variables) – PM 2Ring Apr 06 '17 at 11:25

2 Answers2

1

Maybe you need:

import copy
b = copy.deepcopy(a)
# apply some changes to `a` will not affect `b`
Zing Lee
  • 720
  • 6
  • 10
0

You can use copy_list=old_list[:] - create a slice

>>> a=[1,2,3]
>>> a
[1, 2, 3]
>>> b=a[:]
>>> b
[1, 2, 3]
>>> b.insert(2,5)
>>> b
[1, 2, 5, 3]
>>> a
[1, 2, 3]

It can be seen that on modifying the new list b, the old list a remain unaffected!

Keerthana Prabhakaran
  • 3,766
  • 1
  • 13
  • 23