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 8382907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:02:19+00:00 2026-06-09T17:02:19+00:00

This is driving me bananas :) I am trying to SUM values from a

  • 0

This is driving me bananas 🙂
I am trying to SUM values from a list according to Month, I tried a few things, but badly need guidance.

I am trying to :
For Month 1 – 12.

Iteratively Read PlanWeek(4) values from a list (EC_PlanData), and Sum

Then calculate a smoothed avergae based on summed value.

Here is my code:

G_counter = 1
j = i
m = 1
Plantotal = 0
PlanMonth = 0
DFD = []
EC_PlanData = [500,500.... etc] # 52 values

PlanWeek = range(j,j+3)
Month = range(m,13,1)

## Define Variables
ym, xh, xm, N1, Z1, N2, Z2 = 0,0,0,0,0,0,0

for month in Month:      # for each month 1 - 13
    for i,e in enumerate(list_1):      # read through list
        PlanMonth = PlanMonth + i+3    # sum 4 weekly values
        DFD.append(PlanMonth)          # append sum value to DFD list
        if i == 1:                     # if G_counter = 1, which it always is
            IFX.append(PlanMonth)      # also append to IFX list

    Plantotal= Plantotal+PlanMonth     # calculations here on are
    for i,e in enumerate(DFD):         # evaluated after appending above
        y = e

    ym = Plantotal / m                 # These are calculating a smoothing average
    xh = xh + m
    xm = xh / m      
    N1 = (m-xm) * (y-ym)
    Z1 = (m-xm) * (m-xm)
    N2 = N2 + N1
    Z2 = Z2 + Z1

    if Z2 == 0:                        # Decision on FC value
        FC = 0                         # This or
    else:
        FC = ym -(N2/Z2)*xm + (N2/Z2)*(m+1) # This

    J +=4                              # Advances on 4 time periods
    M +=1                              # Advances on 1 Month
    PlanMonth = 0                      # Resets PlanMonth variable
  • 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-09T17:02:21+00:00Added an answer on June 9, 2026 at 5:02 pm

    You must realize that 12 does not divide 52, and that there are not 4 weeks to every month. So to give an example that you can fine tune to get exactly what you want, I’ve defined a week to belong to the same month that its thursdays belong to. This dovetails nicely with the ISO 8601 definition of the first week of the year. If there’s a week left, then I add that week to december.

    import datetime
    from itertools import groupby
    
    def get_week(date):
        return date.isocalendar()[1]
    
    def group_by_month(weeks, year):
        """
        Group a list containing one item per week, starting with week 1, by month.
    
        If there are too few items to fill a year, stop after last item.
        If there are more items than weeks in the year, stop before new year.
        """
        day = datetime.timedelta(days=1)
        week = datetime.timedelta(days=7)
    
        # Find first Thursday (it's in week 1 by ISO 8601)
        date = datetime.date(year, 1, 1)
        while date.weekday() != 3:
            date += day
    
        # Create list of one day from each week
        thursdays = []
        while date.year == year:
            thursdays.append(date)
            date += week
    
        # Check if the last day is in the last week and if not, 
        # add the week of the last day
        last = tursdays[-1]
        if get_week(last.replace(day=31)) != get_week(last):
            # this will not be a Thursday, but what the hey
            thursdays.append(last.replace(day=31))
    
        # The thursdays are already sorted by month, so 
        # it's OK to use groupby without sorting first
        for k, g in groupby(zip(weeks, thursdays), key=lambda x: x[1].month):
            yield [x[0] for x in g]
    
    list_1 = [500] * 52
    
    print map(sum, group_by_month(list_1, 2012))
    

    Result:

    [2000, 2000, 2500, 2000, 2500, 2000, 2000, 2500, 2000, 2000, 2500, 2000]
    

    You should also be aware of the fact that the year may contain 53 weeks, and if so you must supply a 53-item list instead of a 52-item list. If you don’t, the 53rd week is simply ignored.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is quite literally driving me bananas - I'm on holiday as from tomorrow
This is driving me nuts... I've seen a few posts on similar issues but
This is driving me nuts, maybe I'm missing something but I'm trying to upgrade
This is driving me bananas.. If I select a UITextField in interface builder and
This is driving me insane. I'm trying to change the panels when you click
This is driving me crazy and I've tried a number of different options. I'm
This is driving me crazy. I'm trying to get the footer div to be
This is driving me crazy. I've tried searching for answers and nothing quite matches
This is driving me insane. I am trying to get a basic Report to
This is driving me nuts. I am trying to set a background image 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.