【Python 练习实例44 – Python 两个矩阵相加】两个 3 行 3 列的矩阵,实现其对应位置的数据相加,并返回一个新矩阵
1 2 3 4 5 6 7 8 9 10 |
import numpy as np list_1 = np.random.randint(0,50,(3,3)) list_2 = np.random.randint(0,50,(3,3)) new_list = [[],[],[]] print(list_1, list_2, sep='\n +\n') print(' =') for i in range(len(list_1)): for j in range(len(list_1[i])): new_list[i].append(list_1[i][j] + list_2[i][j]) print(new_list) |
输出:
1 2 3 4 5 6 7 8 9 |
[[40 30 8] [49 3 33] [19 7 12]] + [[45 46 40] [47 42 48] [14 13 32]] = [[85, 76, 48], [96, 45, 81], [33, 20, 44]] |