Skip to content

Note: 2차원 배열 90도 회전 #2

Description

@YimJiYoung
def rotate1(arr):
    return list(zip(*arr[::-1]))
# 참고: https://stackoverflow.com/questions/8421337/rotating-a-two-dimensional-array-in-python

def rotate2(arr):
  n = len(arr) # 행 길이 계산
  m = len(arr[0]) # 열 길이 계산
  result = [[0] * n for _ in range(m)] # 결과 리스트
  for i in range(n):
    for j in range(m):
      result[j][n - i - 1] = arr[i][j]
  return result
  • zip(*iterable): 동일한 개수로 이루어진 자료형을 묶어 주는 역할을 하는 함수
>>> list(zip([1, 2, 3], [4, 5, 6]))
[(1, 4), (2, 5), (3, 6)] # 리스트의 같은 인덱스의 요소끼리 묶임
>>> list(zip([1, 2, 3], [4, 5, 6], [7, 8, 9]))
[(1, 4, 7), (2, 5, 8), (3, 6, 9)]
>>> list(zip("abc", "def"))
[('a', 'd'), ('b', 'e'), ('c', 'f')]
  • * : Unpacking Argument Lists. 리스트의 각 요소를 argument로 넘겨준다

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions