I am trying to determine the cause of 2 identical lists returning different values when assigning a new value. Provided I have a list, a
,
when I assert the value of a
equals class variable, matrix
, I return true. When I change the value of the the first element in each list a
and matrix
, the lists no longer are equal. What about manipulating the list matrix
causes a different result than what is expected?
class Matrix():
def __init__(self, cells):
self.cells: int = cells
self.block: list = []
self.matrix: list = []
self.empty_block()
self.create_matrix()
# empty block
def empty_block(self):
self.block = [list(None for row in range(self.cells)) for col in range(self.cells)]
# matrix of blocks
def create_matrix(self):
self.matrix = [list(self.block for row in range(self.cells)) for col in range(self.cells)]
def insert(self, row, block, sub_column, sub_row, value):
a = [[[[None, None], [None, None]], [[None, None], [None, None]]], [[[None, None], [None, None]], [[None, None], [None, None]]]]
print(self.matrix == a)
a[row][block][sub_column][sub_row] = value
self.matrix[row][block][sub_column][sub_row] = value
print(self.matrix == a)
print(f"a: {a}")
print(f"b: {self.matrix}")
from matrix import Matrix
matrix = Matrix(2)
matrix.insert(0,0,0,0,1)
The result is:
True
False
a: [[[[1, None], [None, None]], [[None, None], [None, None]]], [[[None, None], [None, None]], [[None, None], [None, None]]]]
b: [[[[1, None], [None, None]], [[1, None], [None, None]]], [[[1, None], [None, None]], [[1, None], [None, None]]]]
The expected result is:
The result is:
True
True
a: [[[[1, None], [None, None]], [[None, None], [None, None]]], [[[None, None], [None, None]], [[None, None], [None, None]]]]
b: [[[[1, None], [None, None]], [[None, None], [None, None]]], [[[None, None], [None, None]], [[None, None], [None, None]]]]