I am trying to fill up my matrix
with zeros. Unfortunatelly, in the following example, variables x
and y
are redundand:
self.matrix = [[0 for x in range(0, self.N)] for y in range(0, self.N)]
Multiplying list, copy only references what of course is not what I am expecting:
>>> matrix = [[0] * 5] * 5
>>> matrix
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
>>> matrix[1][1] = "X"
>>> matrix
[[0, 'X', 0, 0, 0], [0, 'X', 0, 0, 0], [0, 'X', 0, 0, 0], [0, 'X', 0, 0, 0], [0, 'X', 0, 0, 0]]
So, is there any solution using a list comprehension to avoid redundand variables (x
& y
)?