Reputation: 61
For example, one list is [[1,2,3],[4,5,6]]
. The second list is [[2,3,4],[3,4,5]]
and then I want to 1 + 2 =3 2+ 3 =5
..... finally it becomes a new list : [[3,5,7],[7,9,11]
and return the new list?
If I have two table, table 1 and table 2, then I create a new table 3 and add the value of each element in table1 to the value of the corresponding element of table2 and store the sum at the same location in table 3
Upvotes: 1
Views: 1083
Reputation: 13700
Option 1: Using list comprehension:
add_matrices = lambda m1,m2: [[x+y for x,y in zip(v1,v2)] for v1,v2 in zip(m1,m2)]
add_matrices ([[1,2,3],[4,5,6]],[[2,3,4],[3,4,5]])
Option 2: Using Numpy
import numpy as np
np.array([[1,2,3],[4,5,6]])+np.array([[2,3,4],[3,4,5]])
Upvotes: 2