2012-08-14 206 views
-5

这是驱动我香蕉:) 我想根据月份从列表中总结值,我尝试了一些东西,但非常需要指导。Python:如果循环嵌套

我想: 对于第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 
+2

PlanMonth最初来自哪里?我希望你在列表中没有真正写52次52次,list_2和list_3在哪里?究竟是什么问题? PlanWeek是什么? – 2012-08-14 09:03:55

+1

目前尚不清楚你想要达到的目标。建议用伪代码描述问题 – 2012-08-14 09:05:13

+0

你会得到什么? – joaquin 2012-08-14 09:05:14

回答

1

你必须认识到,12不分52,和有没有4周每月。因此,为了举一个例子,你可以调整以获得你想要的东西,我已经定义了一个星期属于它的周日所属的同一个月。这与今年第一周的ISO 8601定义非常吻合。如果还有一个星期,那么我将这周添加到十二月。

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周就会被忽略。

+0

哦,哇,从初学者程序员的角度来看,这非常棒,我有很多东西需要学习。但那确实有效。在我看到这个之前,我转贴了我原来的文章。至于53周的情况,我意识到这一点,多年以来Excel),但一举解决了这个问题。 我会看看我是否能够继续平滑的平均部分。伟大的答案btw。 – manengstudent 2012-08-14 12:38:23

+1

@manengstudent为了简化与其他Python编码器的交流,我建议你看一下[python style guide](http://www.python.org/dev/peps/pep-0008/)。阅读符合某种风格(几乎)每个人都知道和遵循的代码更容易,而对于Python来说,就是这样。 – 2012-08-14 12:58:23