1

How could I code a function that adds to the previous index of the array without using a for loop. So for the second value in the Expected Output it will be previous index (2) plus current index(5) resulting in 7. How would I be able to do such a thing?

import numpy as np

A = np.array([2,5,44,-12,3,-5])

Expected output:

[2,7,51,39,42,37]

1 Answer 1

1

Here you go:

np.cumsum(A)

Prints:

array([ 2,  7, 51, 39, 42, 37], dtype=int32)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.