Python :矩阵旋转


# 矩阵顺时针
Map1 = [
    [1,2,3,4,5],
    [6,7,8,9,10],
    [11,12,13,14,15],
    [16,17,18,19,20],
    [21,22,23,24,25]
]
Map1 = Map1[::-1] # 翻转矩阵
Map1 = list(map(list,zip(*Map1))) # *list 解压列表
print(Map1)

# 矩阵逆时针
Map2 = [
    [1,2,3,4,5],
    [6,7,8,9,10],
    [11,12,13,14,15],
    [16,17,18,19,20],
    [21,22,23,24,25]
]
Map2 = list(map(list,zip(*Map2)))
Map2 = Map1[::-1]
print(Map2)

主要是用了zip函数 作用 :zip([1,2,3],[4,5,6]) => [1,4],[2,5],[3,6]

*list => *[[1,2],[3,4]] => [1,2],[3,4]

你可能感兴趣的:(Python,python)