What different between [[0] * n for i in range(n)]
and [[0] * n] * n]
I have this code:
def matrix_decomposition(a):
n = len(a)
b = [[0] * n for i in range(n)]
c = [[0] * n for i in range(n)]
for j in range(0, n):
b[j][0] = a[j][0]
c[0][j] = a[0][j]/b[0][0]
for i in range(1, n):
for j in range(1, i+1):
b[i][j] = a[i][j] - sum(b[i][k]*c[k][j] for k in range(0, i))
for j in range(i, n):
c[i][j] = (a[i][j] - sum(b[i][k]*c[k][j] for k in range(0, i))) / b[i][i]
return b, c
and if I replace [[0] * n for i in range(n)]
on [[0] * n] * n
I get this error:
Traceback (most recent call last): File "C:/Users/Noctus/PycharmProjects/lab3/matrix.py", line 46, in [7, 22, 70, 149] File "C:/Users/Noctus/PycharmProjects/lab3/matrix.py", line 27, in matrix_decomposition c[i][j] = (a[i][j] - sum(b[i][k]*c[k][j] for k in range(0, i))) / b[i][i] ZeroDivisionError: float division by zero
in 46 line I have:
b, c = matrix_decomposition([
[1, 2, 3, 4],
[2, 7, 21, 26],
[4, 13, 43, 88],
[7, 22, 70, 149]
])