Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8628349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:31:05+00:00 2026-06-12T08:31:05+00:00

What I want to do is generate a numpy array that is the cumulative

  • 0

What I want to do is generate a numpy array that is the cumulative sum of another numpy array given a certain window.

For example, given an array [1,2,3,4,5,6,7,8,9,10,11,12] let’s say I want a cumulative sum with a window of 3. What I want as out put would be [1,3,6,9,12,15,18,21,24,27,30,33]. I have a relatively large numpy array and would like to do a cumulative sum with a window of 400.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T08:31:07+00:00Added an answer on June 12, 2026 at 8:31 am
    In [42]: lis=[1,2,3,4,5,6,7,8,9,10,11,12]
    
    In [43]: w=3       #window size
    
    In [44]: [sum(lis[i-(w-1):i+1]) if i>(w-1) else sum(lis[:i+1])  for i in range(len(lis))]
    Out[44]: [1, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33]
    
    In [45]: w=4
    
    In [46]: [sum(lis[i-(w-1):i+1]) if i>(w-1) else sum(lis[:i+1])  for i in range(len(lis))]
    Out[46]: [1, 3, 6, 10, 14, 18, 22, 26, 30, 34, 38, 42]
    

    for python 2.4 or less, change the ternary operator:

    (falseValue, trueValue)[condition] instead of trueValue if condition else falseValue

    [(sum(lis[:i+1]),sum(lis[i-(w-1):i+1]))[i>(w-1)]  for i in range(len(lis))]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a class of 30 students and want generate every possible
I have a very long list in a numpy.array . I want to generate
I want to generate a report to accumulate the total stock quantity that has
I have some python code which generates a 256^3 numpy array of data that
I want generate static pages from database. What is a better suggestion that a
I want to generate an array with 10 elements (0, 1, 2, ..., 9)
Want to generate page events( pageload() ) in jsp is that possible? How to
I'm looking to quickly (hopefully without a for loop) generate a Numpy array of
I have a time series A. I want to generate another time series B,
I want generate array of all locales for all International currencies. I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.