#第一种方法
def add(a, b):
# 下面两行保证输入的a和b是维数相同的矩阵,根据实际情况不要也可以
assert (len(a) == len(b))
assert (all([len(a[i]) == len(b[i]) == len(a[0]) for i in range(len(a))]))
i, j = len(a), len(a[0])
c = [[0] * j] * i
for m in range(i):
for n in range(j):
c[m][n] = a[m][n] + b[m][n]
return c
a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
b = [[9, 8, 7], [6, 5, 4], [3, 2, 1]]
c = add(a, b)
print(c)