Python에서 행렬

발행: (2026년 4월 6일 AM 03:05 GMT+9)
1 분 소요
원문: Dev.to

Source: Dev.to

행렬 정의하기

matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]

3x3 행렬 만들기

matrix_3x3 = [[0]* 3 for in range(3)]

일반적인 행렬 문제

행렬 전치하기

def calculate_transpose_of_matrix(mat: list[list]) -> list[list]:
    N = len(mat)
    for i in range(N):
        for j in range(i+1, N):
            mat[i][j], mat[j][i] = mat[j][i], mat[i][j]

    return mat

행렬을 나선형 패턴으로 출력하기

def print_matrix_in_spiral(mat: list[list]) -> list[list]:
    R = len(mat)
    C = len(mat[0])

    top = 0
    left = 0
    bottom = R - 1
    right = C - 1

    while top <= bottom and left <= right:
        for i in range(left, (right + 1)):
            print(mat[top][i], end=" ")
        top += 1
        for i in range(top, (bottom + 1)):
            print(mat[i][right], end=" ")
        right -= 1
        if top <= bottom:
            for i in range(right, (left - 1), -1):
                print(mat[bottom][i], end=" ")
            bottom -= 1
        if left <= right:
            for i in range(bottom, (top - 1), -1):
                print(mat[i][left], end= " ")
            left += 1
0 조회
Back to Blog

관련 글

더 보기 »

config.py 설정

모든 프로젝트는 같은 방식으로 시작합니다… 몇 가지 값을 hardcode하고, os.getenv 호출을 여기저기 뿌린 뒤, “나중에 정리하겠어”라고 스스로에게 말합니다. 나중은 결코 오지 않습니다. In...

아무도 추천하지 않은 Stack

백엔드: FastAPI 저는 JavaScript와 TypeScript를 사용해 온 개발자로, 프론트엔드에서는 수년간 React를, 백엔드에서는 Express와 Fastify를 사용했습니다. 이 프로젝트를…