-1

I want to know how to get a sum of values in a 3x3 NumPy array with a loop. For example:

[[1,2,3],[4,5,6],[7,8,9]]

The output should be [6,15,24].

1

1 Answer 1

1

Try using np.array.sum with axis argument as one:

arr.sum(1)

Example:

>>> arr=np.array([[1,2,3],[4,5,6],[7,8,9]])
>>> arr.sum(1)
array([ 6, 15, 24])

Or if it is a list:

np.sum(array,1)

Example:

>>> arr=[[1,2,3],[4,5,6],[7,8,9]]
>>> np.sum(arr,1)
array([ 6, 15, 24])
>>> 
Sign up to request clarification or add additional context in comments.

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.