-5

这让我发疯了 :) 我正在尝试根据月份从列表中求和值,我尝试了一些事情,但非常需要指导。

我正在尝试:第 1 个月至第 12 个月。

从列表 (EC_PlanData) 中迭代读取 PlanWeek(4) 值,然后求和

然后根据求和值计算平滑平均值。

这是我的代码:

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
4

1 回答 1

1

您必须意识到 12 不除以 52,并且每个月没有 4 周。所以举一个例子,你可以微调以获得你想要的东西,我已经定义了一周属于它的星期四所属的同一个月。这与 ISO 8601 对一年中第一周的定义非常吻合。如果还有一周,那么我把那一周加到 12 月。

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))

结果:

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

您还应该知道一年可能包含53 周这一事实,如果是这样,您必须提供 53 项列表而不是 52 项列表。如果你不这样做,第 53 周就会被忽略。

于 2012-08-14T10:45:15.150 回答