Я пытаюсь определить причину, по которой два идентичных списка возвращают разные значения при назначении нового значения. При условии, что у меня есть список, a
,
когда я утверждаю, что значение a
равно переменной класса, matrix
, я возвращаю true. Когда я изменяю значение первого элемента в каждом списке a
и matrix
, списки больше не равны. Как насчет манипулирования списком matrix
приводит к результату, отличному от ожидаемого?
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)
Результат:
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]]]]
Ожидаемый результат:
Результат:
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]]]]