I am working on a sudoku solver and I ran into a problem with classes. I still am not familiar with the mechanics of a class so I did this project to help me.
Is there a way for the class to not affect the original list? As shown in the example code, I wanted to change the first element of the list of lists to 11 but only for the mat1
and not the num_matrix
.
Thank you so much in advance!
from pprint import pprint
num_matrix = [
[0, 0, 9, 4, 0, 7, 0, 0, 1],
[0, 0, 0, 0, 3, 0, 0, 7, 0],
[4, 0, 0, 0, 8, 0, 0, 0, 0],
[5, 0, 0, 1, 0, 4, 0, 2, 0],
[0, 0, 3, 0, 0, 0, 5, 0, 0],
[0, 0, 0, 8, 0, 0, 0, 0, 0],
[3, 0, 0, 5, 0, 2, 0, 1, 0],
[0, 0, 0, 0, 7, 0, 0, 0, 0],
[0, 6, 0, 0, 0, 0, 0, 0, 9]]
class hi():
def __init__(self, puzzle):
self.puzzle = puzzle
def replace(self):
self.puzzle[0][0] = 11
mat1 = hi(num_matrix)
mat1.replace()
pprint(num_matrix)
pprint(mat1.puzzle)
and here is the output:
[[11, 0, 9, 4, 0, 7, 0, 0, 1],
[0, 0, 0, 0, 3, 0, 0, 7, 0],
[4, 0, 0, 0, 8, 0, 0, 0, 0],
[5, 0, 0, 1, 0, 4, 0, 2, 0],
[0, 0, 3, 0, 0, 0, 5, 0, 0],
[0, 0, 0, 8, 0, 0, 0, 0, 0],
[3, 0, 0, 5, 0, 2, 0, 1, 0],
[0, 0, 0, 0, 7, 0, 0, 0, 0],
[0, 6, 0, 0, 0, 0, 0, 0, 9]]
[[11, 0, 9, 4, 0, 7, 0, 0, 1],
[0, 0, 0, 0, 3, 0, 0, 7, 0],
[4, 0, 0, 0, 8, 0, 0, 0, 0],
[5, 0, 0, 1, 0, 4, 0, 2, 0],
[0, 0, 3, 0, 0, 0, 5, 0, 0],
[0, 0, 0, 8, 0, 0, 0, 0, 0],
[3, 0, 0, 5, 0, 2, 0, 1, 0],
[0, 0, 0, 0, 7, 0, 0, 0, 0],
[0, 6, 0, 0, 0, 0, 0, 0, 9]]
here is my desired output:
[[0, 0, 9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