본문 바로가기

Coding Test/Programmers

[프로그래머스/파이썬] 행렬의 곱셈

728x90

나의 풀이

def solution(arr1, arr2):
    answer = [[0 for i in range(len(arr2[0]))] for j in range(len(arr1))]

    for y in range(len(arr1)):
        for x in range(len(arr2[0])):
            for i in range(len(arr1[0])):
                answer[y][x] += arr1[y][i] * arr2[i][x]

    return answer

 

 

 

깔끔한 풀이

def productMatrix(A, B):
    return [[sum(a*b for a, b in zip(A_row,B_col)) for B_col in zip(*B)] for A_row in A]

 

 

 

 numpy를 이용한 풀이

import numpy as np
def productMatrix(A, B):

    return (np.matrix(A)*np.matrix(B)).tolist()

 

 

 

 

반응형