题解 | #矩阵乘法#
矩阵乘法
https://www.nowcoder.com/practice/ebe941260f8c4210aa8c17e99cbc663b
简洁明了的代码。
def fun(): # 计算i行j列 def get_i_j_value(i, j): ls_a, ls_b = a[i], [b_row[j] for b_row in b] return sum([ls_a[ret_i] * ls_b[ret_i] for ret_i in range(len(ls_a))]) # 取得矩阵1和2的行列 row1, col1, col2 = int(input().strip()), int(input().strip()), int(input().strip()) # 推导式得到矩阵1和2 a = [list(map(int, input().strip().split(" "))) for _ in range(row1)] b = [list(map(int, input().strip().split(" "))) for _ in range(col1)] # 遍历结果的第i,j位,求值 res = [[get_i_j_value(i, j) for j in range(col2)] for i in range(row1)] # 打印结果 for res_row in res: print(" ".join(list(map(str, res_row)))) fun()