python列表推导实现矩阵乘法

M = [
    [1, 2],
    [3, 4]
]

N = [
    [2, 4],
    [3, 1]
]

n = len(M)
MN = [[sum(M[i][k]*N[k][j] for k in range(n)) for j in range(n)] for i in range(n)]

MN
>> [[8, 6], [18, 16]]

你可能感兴趣的:(python列表推导实现矩阵乘法)