词典中的元组值与2个词典中几个键的总和

时间:2016-04-13 20:40:38

标签: python dictionary sum

我一直试图对此进行一些研究并且已经部分地在那里,但还没有完成。我正在使用2本词典。我的词典如下,第一个字典的价值观代表:办公室开始的年份和服务年限。第二本字典(在那里缩短到3年,太大了,不能把所有东西放在这里),关键代表年份,价值是按月(0到11)失业人数:

pres={"Reagan": (1981, 8), "Bush":(1989, 4),
    "Bill Cliton":(1993,8,}

unemploy=   {
1989:   (6682,  6359,   6205,   6468,   6375,   6577,   6495,   6511,   6590,   6630,   6725,   6667),  
1990:   (6752,  6651,   6598,   6797,   6742,   6590,   6922,   7188,   7368,   7459,   7764,   7901),  
1991:   (8015,  8265,   8586,   8439,   8736,   8692,   8586,   8666,   8722,   8842,   8931,   9198),
1992:   (9283,  9454,   9460,   9415,   9744,   10040,  9850, 9787, 9781,   9398,   9565,   9557)   }

如果我要召集一位总统,例如里根,我一直试图获得平均失业率。我到目前为止所处的位置:

def avg_unemployment_for_president (pres,unemploy,president):
    total=0 
    avg=0
    for key,value in pres.items():
        if key == president:
            firstyear= value[0]
            lastyear=value[0]+ value[1]
            if (firstyear, lastyear) in unemploy.keys():
                for x in range(firstyear,lastyear):

                    allunemp = unemploy[x]
                    listunemp=list(allunemp)
                    total= sum(listunemp)

                    return(total)

avg_unemployment_for_president(pres, unemploy "Reagan")

如何让它从第一年开始迭代,并将每年的元组值中的所有值添加到结束年份?我希望获得的产出是多年来的月平均失业率。

我只做了一年的月平均值:

def avg_unemployment_for_month(unemploy,month):

    sum = 0
    avg=0
    if (0>month) or (month > 11):
        return None
    else:
        for m in unemploy.values():
            sum += m[month]
        average=sum / len(unemploy)
        avg=int(average)
        return (avg)

和一年的总失业率:

def total_unemployment_for_year(unemploy,year): 

    total = 0
    if year in unemploy.keys():
        allunemp = unemploy[year]

        listunemp=list(allunemp)

        totalunemp= sum(listunemp)

        return(totalunemp)

2 个答案:

答案 0 :(得分:0)

对内置sum的一种理解和使用将有所帮助:

avg_unemployment = {
    p: sum(  # sum yearly averages ...
        sum(unemploy[year])/len(unemploy[year])  # monthly average per year
        for year in range(start, start+served)   # for each year of presidency
    )/served  # ... and divide by number of years served
    for p, (start, served) in pres.iteritems()  # for each presidency
}

# for provided years:
# {'Bush': 7958}

答案 1 :(得分:0)

schwobaseggl的答案真的很好而且简短,但如果你需要它的功能格式,请看下面。

pres = {
    "Reagan": (1981, 8),
    "Bush": (1989, 4),
    "Bill Clinton": (1993,8),
}

unemploy = {
    1989:   (6682,  6359,   6205,   6468,   6375,   6577,   6495,   6511,   6590,   6630,   6725,   6667),
    1990:   (6752,  6651,   6598,   6797,   6742,   6590,   6922,   7188,   7368,   7459,   7764,   7901),
    1991:   (8015,  8265,   8586,   8439,   8736,   8692,   8586,   8666,   8722,   8842,   8931,   9198),
    1992:   (9283,  9454,   9460,   9415,   9744,   10040,  9850,   9787,   9781,   9398,   9565,   9557),
}


def avg_unemployment_for_pres_term(name):
    if pres.get(name) is None:
        return 0

    the_pres = pres[name]

    first_year = the_pres[0]
    last_year = first_year + the_pres[1]

    years = []

    for year in range(first_year, last_year):
        unemp_rates = unemploy.get(year, (0,))
        years.append(sum(unemp_rates)/len(unemp_rates))

    return sum(years) / len(years)


def avg_unemployment_for_pres_year(name, year):
    if pres.get(name) is None:
        return 0

    the_pres = pres[name]

    unemp_rates = unemploy.get(year, (0,))
    return sum(unemp_rates)/len(unemp_rates)


print avg_unemployment_for_pres_term("Bush")  # 7958
print avg_unemployment_for_pres_year("Bush", 1989)  # 6523