Python 字典中的元组值在两个字典中的多个键上的总和
我在这方面已经尝试了一段时间,并且已经部分实现了,但还没有完全实现。我在用两本字典。我的字典如下,第一本字典的值代表:从任职开始的年份和服务的年份。第二本字典(那里缩短为3年,太大了,无法在这里把所有的东西都放进去),关键代表年份,数值按月(0到11)表示失业人数:Python 字典中的元组值在两个字典中的多个键上的总和,python,dictionary,sum,Python,Dictionary,Sum,我在这方面已经尝试了一段时间,并且已经部分实现了,但还没有完全实现。我在用两本字典。我的字典如下,第一本字典的值代表:从任职开始的年份和服务的年份。第二本字典(那里缩短为3年,太大了,无法在这里把所有的东西都放进去),关键代表年份,数值按月(0到11)表示失业人数: pres={"Reagan": (1981, 8), "Bush":(1989, 4), "Bill Cliton":(1993,8,} unemploy= { 1989: (6682, 6359, 6205
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)
理解和使用内置的dict将有助于:
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}
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
通过一些修补和删除不必要的部分,我得到了这个完美的工作,只需调用一个定义。