본문 바로가기
CodeSnippet

Rotate Matrix

by cocacola0 2022. 4. 16.
def rotate_matrix(grid):
    return list(map(list, zip(*reversed(grid))))

def print_matrix(grid):
    for row in grid:
        print(row)
        print('----')

grid = [['1', '2'],
        ['.', '0']]

# check original matrix
print_matrix(grid)

# reverse the outer list
print_matrix(list(reversed(grid)))

# zip the reversed list
print_matrix(rotate_matrix(grid))

Ref

https://stackoverflow.com/questions/53242961/rotating-a-list-of-lists

 

Rotating a list of lists

m = [[5,9,1,8], [2,4,5,7], [6,3,3,2], [1,7,6,3]] rotated_map = [] for i in range(len(m[0])): rotated_map.append([x[i] for x in m]) print(rotated_map) """ my

stackoverflow.com

https://stackoverflow.com/questions/53269685/how-to-rotate-a-matrix-nested-list-counter-clockwise-by-90-degrees

 

How to rotate a matrix (nested list) counter clockwise by 90 degrees

I'm trying to rotate a matrix counter clockwise by 90 degrees. For example, if: m = [[1,2,3], [2,3,3], [5,4,3]] then the result should be m = [[3,3,3], [2,3,4], [1,2,5]] ...

stackoverflow.com

 

'CodeSnippet' 카테고리의 다른 글

문자열 분리  (0) 2022.04.25
시뮬레이션 - 방향설정  (0) 2022.04.21

댓글