Amm*_*mar 2 python zip transpose function matrix
我试图定义一个转置矩阵的函数.这是我的代码:
def Transpose (A):
B = list(zip(*A))
return B
Run Code Online (Sandbox Code Playgroud)
现在,当我在程序中的某个地方调用函数时,如下所示:
Matrix = [[1,2,3],[4,5,6],[7,8,9]]
Transpose(Matrix)
print(Matrix)
Run Code Online (Sandbox Code Playgroud)
矩阵没有变化.我究竟做错了什么?
您的函数返回一个不影响矩阵的新值(zip不会更改它的参数).你没有做错任何事,这是正确的做事方式.只需将其更改为:
print(Transpose(Matrix))
Run Code Online (Sandbox Code Playgroud)
要么
Matrix = Transpose(Matrix)
Run Code Online (Sandbox Code Playgroud)
注意:您确实应该为函数和变量使用小写名称.