Python 练习实例44 - Python 两个矩阵相加
两个 3 行 3 列的矩阵,实现其对应位置的数据相加,并返回一个新矩阵:
X = [[12,7,3],
[4 ,5,6],
[7 ,8,9]]
Y = [[5,8,1],
[6,7,3],
[4,5,9]]
程序分析:创建一个新的 3 行 3 列的矩阵,使用 for 迭代并取出 X 和 Y 矩阵中对应位置的值,相加后放到新矩阵的对应位置中。
程序源代码:
源代码:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
X = [[12,7,3],
[4 ,5,6],
[7 ,8,9]]
Y = [[5,8,1],
[6,7,3],
[4,5,9]]
result = [[0,0,0],
[0,0,0],
[0,0,0]]
# 迭代输出行
for i in range(len(X)):
# 迭代输出列
for j in range(len(X[0])):
result[i][j] = X[i][j] + Y[i][j]
for r in result:
print(r)
执行以上代码,输出结果如下:
[17, 15, 4] [10, 12, 9] [11, 13, 18]
Python 100例
super
sup***guo555@126.com
参考方法:
#!/usr/bin/python # -*- coding: UTF-8 -*- x = [[12,7,3], [4,5,6], [7,8,9]] y = [[5,8,1], [6,7,3], [4,5,9]] z= [] for i in range(3): z.append([]) for i in range(3): for j in range(3): z[i].append(x[i][j]+y[i][j]) print zsuper
sup***guo555@126.com
lll
279***0052@qq.com
参考方法:
#!/usr/bin/python # -*- coding: UTF-8 -*- x=[[12,7,3], [4 ,5,6], [7 ,8,9]] y=[[5,8,1], [6,7,3], [4,5,9]] c=[] d=[] for index,element in enumerate(x): for i in range(len(element)): m=x[index][i]+y[index][i] c.append(m) if len(c)==3: d.append(c) c=[] print dlll
279***0052@qq.com
朦胧
253***5732@qq.com
参考方法:
# coding:utf-8 import numpy as np X = [[12,7,3], [4 ,5,6], [7 ,8,9]] Y = [[5,8,1], [6,7,3], [4,5,9]] Z=np.zeros(shape=(len(X),len(X[0]))) for i in range(0,len(X)): for j in range(0,len(X[0])): Z[i][j]=X[i][j]+Y[i][j] print(Z)朦胧
253***5732@qq.com
qingfeng
297***734@qq.com
参考方法:
#!/usr/bin/python # -*- coding: UTF-8 -*- X = [[12,7,3], [4 ,5,6], [7 ,8,9]] Y = [[5,8,1], [6,7,3], [4,5,9]] m = [[], [], []] for (i,j,k) in zip(X,Y,range(3)): for (a,b)in zip(i,j): m[k].append(a+b) for r in m: print rqingfeng
297***734@qq.com
du_solong
157***7972@qq.com
使用 numpy:
#!/usr/bin/python # coding:utf-8 import numpy as np x = np.array( [[12,7,3], [4 ,5,6], [7 ,8,9]]) y = np.array( [[5,8,1], [6,7,3], [4,5,9]]) z = x+y print zdu_solong
157***7972@qq.com
hello world
1@q***om
Python3 使用 random 随机生成两个矩阵:
import random data1 = [[random.randint(1,100) for i in range(3)] for j in range(3)] data2 = [[random.randint(1,100) for i in range(3)] for j in range(3)] print("data1:",data1) print("data2:",data2) data3 = data1[:] for i in range(len(data1)): for j in range(len(data1[i])): data3[i][j] = data1[i][j] + data2[i][j] print("data3:",data3)hello world
1@q***om
ray
117***0890@qq.com
参考方法:
# -*- coding: utf-8 -*- def matrix(): X = [[12,7,3], [4 ,5,6], [7 ,8,9]] Y = [[5,8,1], [6,7,3], [4,5,9]] a_list=[] a_list_1=[] a_list_2=[] a_list_3=[] for i in range(3): for j in range(3): if i==0: temp=a_list_1 elif i==1: temp=a_list_2 else: temp=a_list_3 temp.append(X[i][j]+Y[i][j]) a_list.insert(0,a_list_1) a_list.insert(1,a_list_2) a_list.insert(2,a_list_3) print(a_list) matrix()ray
117***0890@qq.com
sun_shine
101***0300@qq.com
参考方法:
sun_shine
101***0300@qq.com
奚适
xnx***7@sina.com
使用匿名函数:
#!/usr/bin/python # -*- coding: UTF-8 -*- X = [[12,7,3], [4,5,6], [7,8,9]] Y = [[5,8,1], [6,7,3], [4,5,9]] Z = [] for i in range(3): zz = map(lambda a,b:a+b, X[i],Y[i]) Z.append(zz) print Z奚适
xnx***7@sina.com