0

I am trying to wrap my head around mutable object passed in function. In the code below, solutionList and outSoln return two different lists. From my understanding, the append function appends by reference. I tried to use solutionList.append(sudoku[:]) and solutionList.append(list(sudoku)) as done in other SO answers but it does not seem to work. Is there a better way to do that assigning two global variables?

code:

def solve(options, onlyOneSoln=False):

    global sudoku
    global solutionList
    global outSoln

    if len(solutionList) > 5:
        return True

    for y in range(9):
        for x in range(9):
            # print("x: %s" %x)
            if sudoku[y][x]==0:
                for val in range(1,10):
                    if len(solutionList)>0 and onlyOneSoln:
                            break
                    if possible(val,y, x, sudoku,  options):
                        sudoku[y][x]=val
                        solve(options, prnt, onlyOneSoln)
                        sudoku[y][x]=0
                        # print_sudoku(sudoku)
                        # print("could not assign value -- backtracking")

                return


    solutionList.append(sudoku[:])
    outSoln =copy.deepcopy(solutionList)


sudoku = [[0, 0, 0, 0, 0, 8, 0, 0, 0],
 [1, 0, 0, 0, 0, 0, 0, 4, 0],
 [0, 0, 0, 0, 0, 0, 2, 0, 6],
 [0, 4, 7, 0, 5, 0, 0, 9, 0],
 [8, 0, 0, 6, 0, 3, 0, 0, 5],
 [0, 0, 0, 0, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 6, 5, 0, 0],
 [0, 0, 8, 0, 0, 2, 9, 0, 0],
 [9, 7, 0, 0, 0, 0, 0, 0, 1]]


solutionList = []
outSoln= []
solve([''])

print("outsoln:")
print(outSoln)
print("\nsolutionList:")
print(solutionList)

output:

outsoln:
[[[2, 3, 4, 1, 6, 8, 7, 5, 9], [1, 5, 6, 2, 7, 9, 8, 4, 3], [7, 8, 9, 3, 4, 5, 2, 1, 6], [6, 4, 7, 8, 5, 1, 3, 9, 2], [8, 2, 1, 6, 9, 3, 4, 7, 5], [3, 9, 5, 4, 2, 7, 1, 6, 8], [4, 1, 2, 9, 3, 6, 5, 8, 7], [5, 6, 8, 7, 1, 2, 9, 3, 4], [9, 7, 3, 5, 8, 4, 6, 2, 1]], [[2, 3, 4, 1, 6, 8, 7, 5, 9],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0], [8, 0, 0, 6, 0, 3, 0, 0, 5], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 6, 5, 0, 0], [0, 0, 8, 0, 0, 2, 9, 0, 0], [9, 7, 0, 0, 0, 0, 0, 0, 1]], [[0, 0, 0, 0, 0, 8, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 4, 0], [0, 0, 0, 0, 0, 0, 2, 0, 6], [0, 4, 7, 0, 5, 0, 0, 9, 0], [8, 0, 0, 6, 0, 3, 0, 0, 5], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 6, 5, 0, 0], [0, 0, 8, 0, 0, 2, 9, 0, 0], [9, 7, 0, 0, 0, 0, 0, 0, 1]], [[0, 0, 0, 0, 0, 8, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 4, 0], [0, 0, 0, 0, 0, 0, 2, 0, 6], [0, 4, 7, 0, 5, 0, 0, 9, 0], [8, 0, 0, 6, 0, 3, 0, 0, 5], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 6, 5, 0, 0], [0, 0, 8, 0, 0, 2, 9, 0, 0], [9, 7, 0, 0, 0, 0, 0, 0, 1]]]
Jrakru56
  • 1,211
  • 9
  • 16
  • The answer is in your title: Use `copy.deepcopy()` – Barmar May 21 '20 at 01:28
  • `sudoku[:]` is a shallow copy. – Barmar May 21 '20 at 01:28
  • @Barmar That's the way i did it for `outsoln`. My question was more in the line of whether or not there was a way to this without having to create 2 global variables? Something like `solutionlist = copy.deepcopy(solutionlist.append(sudoku))` does not seem to work. – Jrakru56 May 21 '20 at 01:33
  • `sudoku[:]` is the copy that needs to be changed. – user2357112 May 21 '20 at 01:37
  • In case it matters or you're curious, the performance of `deepcopy` is horrendous. If you have a 2d list, `[x[:] for x in lst]` is much faster. – ggorlen May 21 '20 at 01:41
  • @ggorlen Thank! for the heads up. – Jrakru56 May 21 '20 at 01:43
  • @Jrakru56 That doesn't work because `append()` operates in place, it doesn't return the list. And even if it did, you've already modified it before passing it to deepcopy, so it's too late. – Barmar May 21 '20 at 01:44
  • Are you sure you need to make a copy of the solution list? – Barmar May 21 '20 at 01:45
  • This piece of code does the trick `solutionList.append(sudoku) solutionList = copy.deepcopy(solutionList)` . I am not sure why this is different from `solutionlist = copy.deepcopy(solutionlist.append(sudoku))`. – Jrakru56 May 21 '20 at 01:45
  • @Barmar I see! That answers my comment above. Thank you for the clarification. – Jrakru56 May 21 '20 at 01:46

0 Answers0