I'm trying to invert (transpose) a 2-D square array in python. I know there are ways to do this with numpy, but since I have just a square array, I thought it could be simply done with the following:
array = [[1,2,3],[4,5,6],[7,8,9]]
invert = array
for i in range(len(array)):
for j in range(len(array[0])):
invert[j][i] = array[i][j]
However, when I run this and print the result, I get the following array
[1, 2, 3]
[2, 5, 6]
[3, 6, 9]
It seems to be partially inverted (first column is first row), but not fully (first row is still first row).
The code is very simple, I just start with inverse being the same as array, and then replace all inverse[j,i] with array[i,j]. Not sure why this doesn't work
Just correct your second line :
In your case, the 2 lists invert and array correspond to the same element and are modified simultaneously.