我有以下python函数试图转置一个8x8矩阵,A,将其值存储在零8x8矩阵B中,并返回B.我意识到最可读和/或算法上有效的实现只涉及两个循环,但为了更大的程序我试图建立转置函数需要采用与以下类似的格式:
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
当我在最里面的for循环的每次迭代中打印值时,它会打印出正确的值(您可以在家中验证):
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
但随后的print和return语句产生
[[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]]
循环内打印值与返回值之间的差异是什么原因?