【发布时间】:2019-01-18 00:10:36
【问题描述】:
我正在尝试在函数中交换局部变量以将 2 x 2 矩阵旋转 90 度。
def twobytwo(m):
last = len(m)-1
for i in range(0, last):
swap(m[i][i], m[i][last])
swap(m[i][i], m[last][last])
swap(m[i][i], m[last][i])
return m
def swap(i, j):
temp = i
i = j
j = temp
print(twobytwo([[0, 1], [2, 3]]))
目前,我返回了原始矩阵,但我想查看
[[2,0],[3,1]]
【问题讨论】:
-
swap不会更改twobytwo中的任何内容,因为int是不可变的。 -
@Tomothy32:即使整数是可变的,或者如果您传入列表而不是整数,它也不会改变任何内容。局部变量赋值不会改变对象。
-
关于如何解决此问题的任何建议?
标签: python arrays matrix data-structures