Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:嵌套If循环_Python_Loops_If Statement - Fatal编程技术网

Python:嵌套If循环

Python:嵌套If循环,python,loops,if-statement,Python,Loops,If Statement,这让我抓狂: 我试图根据月份从列表中求和,我尝试了一些事情,但急需指导 我正在努力: 第1-12个月 从列表中迭代读取PlanWeek4值,然后求和 然后根据求和值计算平滑平均值 这是我的密码: 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

这让我抓狂: 我试图根据月份从列表中求和,我尝试了一些事情,但急需指导

我正在努力: 第1-12个月

从列表中迭代读取PlanWeek4值,然后求和

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

这是我的密码:

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

你必须意识到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项清单,而不是52项清单。如果不这样做,第53周将被忽略。

PlanMonth最初来自哪里?我希望你不是真的在那个列表中写了500 52次,列表2和列表3在哪里?到底是什么问题?“计划周”的目的是什么?不清楚你想要实现什么。建议用伪代码描述问题。您能指定任务吗?您是否只需要为给定月份的第3个列表元素求和?第1个月的PlanMonth是指从1到4的值的总和。第2个月的PlanMonth是指从5到8等值的总和,因此列表2和列表3变为13个总和值。我希望你不是真的在那张单子上写了500次52次。。不,这是自动生成的,但为了验证起见,我将数字固定为500。@Manang Student请通过单击链接更新您的答案,并重新编排以使其更清晰,并在评论中回答问题。还要确保示例代码可以作为独立脚本运行,而不会引发异常。哦,哇,这太棒了,从初学者的角度来看,我有很多东西要学习。但这确实有效。在我看到这个之前,我已经转载了我原来的帖子。至于53周的情况,我知道,在Excel工作了很多年;,但你一举解决了这个问题。我会看看我是否可以继续你提供的平滑平均部分的工作。很好的回答。顺便说一句,@manengstudent可以简化与其他python代码编写者的沟通,我建议您看看。阅读符合几乎所有人都知道并遵循的风格的代码更容易,对于python来说,就是这样。