【每日一题20220602】矩阵加法

:mage:‍ 请编写一个函数,接收两个NxN的数字矩阵(二维列表),对它们进行求和。

【示例】
输入:[[1,2,3],[3,2,1],[1,1,1]], [[2,2,1],[3,2,3],[1,1,3]]
输出:[[3,4,4],[6,4,4],[2,2,4]]
解释:

|1 2 3|     |2 2 1|     |1+2 2+2 3+1|     |3 4 4|
|3 2 1|  +  |3 2 3|     |3+3 2+2 1+3|     |6 4 4|
|1 1 1|     |1 1 3|     |1+1 1+1 1+3|     |2 2 4|

题目难度:简单
题目来源:codewars-Matrix Addition

def solution(a: list, b: list)-> list:
    # your code here

assert solution([[1,2],[1,2]], [[2,3],[2,3]]) == [[3,5],[3,5]]
assert solution([[1]], [[2]]) == [[3]]
assert solution([[1,2,3],[3,2,1],[1,1,1]], [[2,2,1],[3,2,3],[1,1,3]]) == [[3,4,4],[6,4,4],[2,2,4]]
def solution(a: list, b: list) -> list:
    # your code here
    for i in range(len(a)):
        a[i] = [e + b[i][x] for x, e in enumerate(a[i])]
    return a


assert solution([[1, 2], [1, 2]], [[2, 3], [2, 3]]) == [[3, 5], [3, 5]]
assert solution([[1]], [[2]]) == [[3]]
assert solution([[1, 2, 3], [3, 2, 1], [1, 1, 1]], [[2, 2, 1], [3, 2, 3], [1, 1, 3]]) == [[3, 4, 4], [6, 4, 4], [2, 2, 4]]
1 个赞
def solution(a: list, b: list) -> list:
    # your code here
    new_list = []
    for i in range(len(a)):
        tmp_list = []
        for j in range(len(a)):
            tmp = a[i][j] + b[i][j]
            tmp_list.append(tmp)
        new_list.append(tmp_list)
    return new_list


assert solution([[1, 2], [1, 2]], [[2, 3], [2, 3]]) == [[3, 5], [3, 5]]
assert solution([[1]], [[2]]) == [[3]]
assert solution([[1, 2, 3], [3, 2, 1], [1, 1, 1]], [[2, 2, 1], [3, 2, 3], [1, 1, 3]]) == [[3, 4, 4], [6, 4, 4],
                                                                                          [2, 2, 4]]

def solution(a:list,b:list):
ll=
for i in range(len(a)):
ls=
for j in range(len(a[i])):
c=a[i][j]+b[i][j]
ls.append(c)
ll.append(ls)
return ll

assert solution([[1, 2], [1, 2]], [[2, 3], [2, 3]]) == [[3, 5], [3, 5]]
assert solution([[1]], [[2]]) == [[3]]
assert solution([[1, 2, 3], [3, 2, 1], [1, 1, 1]], [[2, 2, 1], [3, 2, 3], [1, 1, 3]]) == [[3, 4, 4], [6, 4, 4], [2, 2, 4]]

def solution(a: list, b: list)-> list:
    return [[x+y for x,y in zip(v,b[k])] for k,v in enumerate(a)]
def solution(a: list, b: list):
    for i in range(len(a)):
        b[i] = [value + b[i][index] for index, value in enumerate(a[i])]
    return b
def solution(a: list, b: list)-> list:

    list2=[]
    for i in range(len(a)):
        list1=[]
        for j in range(len(a[i])):
            list1.append(a[i][j]+b[i][j])
        list2.append(list1)
    return list2

assert solution([[1,2],[1,2]], [[2,3],[2,3]]) == [[3,5],[3,5]]
assert solution([[1]], [[2]]) == [[3]]
assert solution([[1,2,3],[3,2,1],[1,1,1]], [[2,2,1],[3,2,3],[1,1,3]]) == [[3,4,4],[6,4,4],[2,2,4]]
def solution(a: list, b: list)-> list:
    return [[sum(j) for j in list(zip(a[i], b[i]))] for i in range(len(a))]

assert solution([[1,2],[1,2]], [[2,3],[2,3]]) == [[3,5],[3,5]]
assert solution([[1]], [[2]]) == [[3]]
assert solution([[1,2,3],[3,2,1],[1,1,1]], [[2,2,1],[3,2,3],[1,1,3]]) == [[3,4,4],[6,4,4],[2,2,4]]
def solution(a: list, b: list)-> list:
    #解法一
    for i in range(len(a)):
        for j in range(len(a[i])):
            a[i][j]=a[i][j]+b[i][j]
    return a
    #解法二
     import  numpy as np
     return np.matrix.tolist(np.mat(a)+np.mat(b))
def solution(a: list, b: list) -> list:
    # your code here
    # 解法一:使用zip把a,b列表一一对应求和并转为列表
    # l = [[a+b for a, b in zip(v, b[k])] for k, v in enumerate(a)]
    # return l
    # 解法二:把a列表元素逐一添加到b列表元素并求和
    # for k, v in enumerate(a):
    #     for j in range(len(v)):
    #         b[k][j] += a[k][j]
    b = [[b[k][j]+a[k][j] for j in range(len(v))] for k, v in enumerate(a)]
    return b


assert solution([[1,2],[1,2]], [[2,3],[2,3]]) == [[3,5],[3,5]]
assert solution([[1]], [[2]]) == [[3]]
assert solution([[1,2,3],[3,2,1],[1,1,1]], [[2,2,1],[3,2,3],[1,1,3]]) == [[3,4,4],[6,4,4],[2,2,4]]
def solution(a: list, b: list)-> list:
    for i in range(len(a)):
        for j in range(len(a[i])):
            a[i][j]+=b[i][j]
    return a

assert solution([[1,2],[1,2]], [[2,3],[2,3]]) == [[3,5],[3,5]]
assert solution([[1]], [[2]]) == [[3]]
assert solution([[1,2,3],[3,2,1],[1,1,1]], [[2,2,1],[3,2,3],[1,1,3]]) == [[3,4,4],[6,4,4],[2,2,4]]