I have a matrix which i want to transpose and I've done this before and it's worked, but it's only today while I've been going through my code and updating it that it has suddenly stopped working. I really don't know why, as it has worked fine before. Here's the main bit of code
class Matrix:
def __init__(self, cols, rows):
self.cols = cols
self.rows = rows
self.matrix = [[0]*self.rows]]*self.cols
def transpose(self):
transposed_matrix = Matrix(self.rows, self.cols)
for i in range(self.cols):
for j in range(self.rows):
transposed_matrix.matrix[j][i] = self.matrix[i][j]
return transposed_matrix
So say I have a matrix like this
[
[0, 1, 1, 0, 0, 0]
[1, -1, 0, 0, 0, -1]
[-1, -1, 0, -1, 1, 0]
[-1, 1, -1, -1, -1, 0]
]
I'm expecting
[
[0, 1, -1, -1]
[1, -1, -1, 1]
[1, 0, 0, -1, -1]
[0, 0, -1, -1]
[0, 0, 1, -1]
[0, -1, 0, 0]
]
But instead I'm getting
[
[0, -1, 0, 0]
[0, -1, 0, 0]
[0, -1, 0, 0]
[0, -1, 0, 0]
[0, -1, 0, 0]
[0, -1, 0, 0]
]
If you can help shed some light on this then that would be really appreciated