题解 | #牛牛的矩阵乘法#
牛牛的矩阵乘法
http://www.nowcoder.com/practice/4356eb31276b4b7689a4cf0997647f88
X_list = [[13,-1,4],[5,0,6],[1,9,-3]]
Y_list = copy.deepcopy(X_list) #将X列表深拷贝给Y列表
new_list = list() #定义新列表存放相乘后结果
''' Y列表中每个元素加1(题目输出结果反推Y矩阵与X矩阵相同,故注释此段代码)
for i in range(len(Y_list)):
for j in range(len(Y_list[i])):
Y_list[i][j] += 1
'''
#题目输出描述为矩阵外积,题干描述求内积,为与输出一致此处求外积:
for row in range(len(X_list)):
row_list = list() #用于存放X矩阵与Y矩阵行列相乘相加的值
for col in range(len(X_list[row])):
sums = 0
for z in range(len(Y_list[row])):
value = X_list[row][z] * Y_list[z][col]
sums += value
row_list.append(sums)
new_list.append(row_list)
print(new_list)