简体   繁体   中英

Sequentially add elements in numpy array into new array

I'm curious if there is a built in function to transform an array of values into a cumulative array of values.

Example:

input = np.asarray([0.000,1.500,2.100,5.000])

into

[0.000,1.500,3.600,8.600]

Thanks!

Use in-built cumsum from NumPy to get the cumulative sum of your array inputt as

inputt = np.asarray([0.000,1.500,2.100,5.000])
print (np.cumsum(inputt)) 

# [0.  1.5 3.6 8.6]

I renamed your array because input is already an in-built function in python to get the user input from the keyboard

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM