Python 내장함수로 2차원 리스트 transpose 및 rotate 해주는 방법

ewillwin·2023년 7월 14일
0

아무거나

목록 보기
20/23

traspose

list(map(list, zip(*A)))

반시계방향 90도 회전

list(reversed(list(map(list, zip(*A)))))

시계방향 90도 회전

A = reversed(A)
A = list(map(list, zip(*A)))

테스트 코드 및 결과

A = [[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]]
print("original: ", A); print()

A = list(map(list, zip(*A))) # transpose
print("Transpose: ", A); print()

A = [[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]]
A = list(reversed(A)) # reversed
print("Reversed: ", A); print()

A = [[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]]
A = list(reversed(list(map(list, zip(*A))))) #반시계방향 90도
print("반시계방향: ", A); print()

A = [[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]]
A = reversed(A)
A = list(map(list, zip(*A))) #시계방향 90도
print("반시계방향: ", A); print()
➜  연습용 python3 test.py
original:  [[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]]

Transpose:  [[1, 6, 11, 16, 21], [2, 7, 12, 17, 22], [3, 8, 13, 18, 23], [4, 9, 14, 19, 24], [5, 10, 15, 20, 25]]

Reversed:  [[21, 22, 23, 24, 25], [16, 17, 18, 19, 20], [11, 12, 13, 14, 15], [6, 7, 8, 9, 10], [1, 2, 3, 4, 5]]

반시계방향:  [[5, 10, 15, 20, 25], [4, 9, 14, 19, 24], [3, 8, 13, 18, 23], [2, 7, 12, 17, 22], [1, 6, 11, 16, 21]]

반시계방향:  [[21, 16, 11, 6, 1], [22, 17, 12, 7, 2], [23, 18, 13, 8, 3], [24, 19, 14, 9, 4], [25, 20, 15, 10, 5]]
profile
💼 Software Engineer @ LG Electronics | 🎓 SungKyunKwan Univ. CSE

0개의 댓글