2012-08-14 204 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