Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python element wise add in list of list with other list

Tags:

python

numpy

I checked similar problem at sum of N lists element-wise python

but mine is a little bit more complicate cause I am adding list of list with another list of list

my list looks like it below

[[0         0.014285714 0.035600016]
,[0.014285714   0           0.038359389]
,[0.035600016   0.038359389 0]]


[[0         0.014285714 0.035600016]
,[0.014285714   0           0.038359389]
,[0.035600016   0.038359389 0]]

so adding these two then results has 3 by 3 matrix

how could I efficiently add these two lists?

like image 210
JonghoKim Avatar asked Dec 12 '25 23:12

JonghoKim


1 Answers

Can i solve using numpy:

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

>>> from numpy import array, sum

>>> list(array(a) + array(b))
[array([5, 7, 9]), array([5, 7, 9]), array([5, 7, 9])]

OR

>>> sum([a, b], axis=0)
 array([[5, 7, 9],
       [5, 7, 9],
       [5, 7, 9]])
>>>
like image 171
James Avatar answered Dec 15 '25 13:12

James



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!