1. 程式人生 > >python 矩陣順時針旋轉90度

python 矩陣順時針旋轉90度

# 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