python 二维数组 行列转换

arr = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
 
#方法1
ar2 =[]
for i in range(len(arr[0])):
    b =[]
    for j in range(len(arr)):
        b.append(arr[j][i])
    ar2.append(b)
print(ar2)
 
#方法2
ar2 = list(map(list,zip(*arr)))
print(ar2)
 
#方法3
ar2 = [ [row[i] for row in arr] for i in range(len(arr[0])) ]
print(ar2)

 

posted @ 2020-11-24 18:23  月夜_1  阅读(2855)  评论(0编辑  收藏  举报