1

I have a numpy stacked array like so:

[[10. 12. 15. 20. 24. 20.]
 [40. 48. 60. 20. 24. 20.]]

How can I add each of the elements together and create a 1D array so that it becomes:

[50. 60. 75. 40. 48. 40]

I also need this this to work no matter how many lists are in the stacked array. Any help would be appreciated.

0

2 Answers 2

2

Use array.sum(axis=0)

a = np.array([[10., 12., 15., 20., 24., 20.],
              [40., 48., 60. ,20., 24., 20.]])

a.sum(axis=0)

Or

np.sum(a,axis=0)

Both gives

array([50., 60., 75., 40., 48., 40.])
Sign up to request clarification or add additional context in comments.

1 Comment

or np.sum(python_list, axis=0)
0

A "pure Python" solution (i.e., non-numpy-based), might be:

summed_list_1D = list(map(sum, zip(*python_list_2D)))

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.