Add two matrices in python

前端 未结 4 1448
独厮守ぢ
独厮守ぢ 2020-12-17 15:45

I\'m trying to write a function that adds two matrices to pass the following doctests:

  >>> a = [[1, 2], [3, 4]]
  >>> b = [[2, 2], [2, 2]         


        
4条回答
  •  失恋的感觉
    2020-12-17 16:23

    def addM(a, b):
        res = []
        for i in range(len(a)):
            row = []
            for j in range(len(a[0])):
                row.append(a[i][j]+b[i][j])
            res.append(row)
        return res
    

提交回复
热议问题