python 矩阵顺时针旋转90度

时间:2024-03-11 09:17:55
# 4*4矩阵旋转90度
def matrix_transposition(data):
    for index,row in enumerate(data):
        for col in range(index,len(row)):
            temp = data[index][col]
            data[index][col] = data[col][index]
            data[col][index] = temp
    return data

if __name__ == \'__main__\':
    data = [[ col for col in range(4)] for row in range(4)]
    print(data)
    print("----------------")
    transpose_data = matrix_transposition(data)
    print(transpose_data)
View Code