I have the following python function attempting to transpose an 8x8 matrix, A, store its values in the zero 8x8 matrix, B, and return B. I realize that the most readable and/or algorithmically efficient implementation would involve only two for loops, but for the sake of the larger program I'm trying to build the transpose function needs to be in a similar format to the following:
def transpose(A, B, n):
tmp = [[0, 0, 0, 0, ]]
for m in range(4):
j_start = m*2
j_finish = j_start + 2
for k in range(4):
i_start = k*2
i_finish = i_start + 2
for i in range(i_start, i_finish):
for j in range(j_start, j_finish):
tmp = A[j][i]
B[i][j] = tmp
print "At B[" + str(i) + "][" + str(j) + "] = " + str(A[j][i])
print B
return B
When I print the value at each iteration of the innermost for loop, it prints out the correct value (which you can verify at home):
At B[0][0] = 1
At B[0][1] = 9
At B[1][0] = 2
At B[1][1] = 10
At B[2][0] = 3
At B[2][1] = 11
....
At B[6][7] = 63
At B[7][6] = 56
At B[7][7] = 64
but the ensuing print and return statements yield
[[8, 16, 24, 32, 40, 48, 56, 64],
[8, 16, 24, 32, 40, 48, 56, 64],
[8, 16, 24, 32, 40, 48, 56, 64],
[8, 16, 24, 32, 40, 48, 56, 64],
[8, 16, 24, 32, 40, 48, 56, 64],
[8, 16, 24, 32, 40, 48, 56, 64],
[8, 16, 24, 32, 40, 48, 56, 64],
[8, 16, 24, 32, 40, 48, 56, 64]]
What's the reason for the disparity between printed values from within the loop and those that are returned?