Python计算两个矩阵的和


使用Python计算两个矩阵的和,并打印结果;

使用嵌套列表推导来遍历矩阵中的每个元素:

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

result = [[X[i][j] + Y[i][j]  for j in range
           
(len(X[0]))] for i in range(len(X))]

for r in result: 
    print(r) 

转载声明
本文版权归作者所有

如需转载,请注明出处;本文地址: https://www.perfcode.com/p/python-program-add-two-matrices.html