1. 方法一
lst1=[ [2,0,0,2], [2,1,2,1], [3,1,1,2], [0,1,0,1], ] lst1[:]=[list(reversed(item)) for item in lst1] print(lst1)
2. 方法二
lst2=[ [2,0,0,2], [2,1,2,1], [3,1,1,2], [0,1,0,1], ] lst2[:]=[list(item) for item in zip(*lst2)] print(lst2)
3. 方法三
import numpy as np # 定义一个矩阵 matrix = np.array([ [2,0,0,2], [2,1,2,1], [3,1,1,2], [0,1,0,1], ]) # 对矩阵进行转置 transpose_matrix = np.transpose(matrix) matrix[:]=transpose_matrix.tolist() print(matrix)
4. 方法四
lst3=[ [2,0,0,2], [2,1,2,1], [3,1,1,2], [0,1,0,1], ] #针对行数与列数相等的矩阵,对角线上的元素与对角线下的互换,lst3[i][j]<--->lst3[j][i] for row in range(len(lst3)): for col in range(row+1,len(lst3)): print((row,col)) if row<=len(lst3)-1 and col<=len(lst3)-1: lst3[row][col],lst3[col][row]=lst3[col][row],lst3[row][col] [print(item,end="\n") for item in lst3]
小结:简单记录一下知识点,注意zip内置函数的用法。
标签:lst3,matrix,lst1,转置,矩阵,python,print,lst2,row From: https://www.cnblogs.com/wancy/p/17492054.html