这让我开车香蕉:) 我试图根据月份从列表中的SUM值,我尝试了一些事情,但非常需要指导。
我想: 第1 - 12个月。
从列表中逐步读取PlanWeek(4)值(EC_PlanData)和Sum
然后根据总和值计算平滑的avergae。
这是我的代码:
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
答案 0 :(得分: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 weeks,如果是,您必须提供53项目列表而不是52项目列表。如果不这样做,那么第53周就会被忽略。