Home > Blockchain >  How do I sum the content of a list containing numpy arrays?
How do I sum the content of a list containing numpy arrays?

Time:01-26

Consider the following code:

arr = np.array([[1, 2, 3],[2, 3, 3]])

list1 = arr.tolist()
print(list1)
[[1, 2, 3], [2, 3, 3]]

I want to create a sum of the entire content to get the following output

list_sum=[[3, 5, 6]]

Is there an elegant way to quickly do this?

CodePudding user response:

You can just use numpy's native summing with an axis:

In [39]: arr.sum(axis=0)
Out[39]: array([3, 5, 6])
  •  Tags:  
  • Related