私は少しこれを処理しようとしてきましたが、部分的にはなってきましたが、まだかなりはありません。私は2つの辞書で作業しています。私の辞書は次のとおりです。最初の辞書の値は立っています。 2番目の辞書(ここでは3年に短縮され、ここにすべてを置くには大きすぎます)は、キーとは年を表し、値は月ごと(0〜11)です:2つのdicitonariesからのいくつかのキーの上の辞書のタップ値の合計
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) }
私は、例えばレーガンのような1人の大統領を呼び出すならば、平均失業率を得ようとしています。どこで私は今のところ得ている:
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")
どのように私はそれが最初の年から反復処理と終了年度までの各年度のtupled値にすべての方法をすべての値を追加するために得ることができますか?私が得ようとしている成果は、何年にもわたって毎月の失業率です。
私はちょうど1年の月平均やった:
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)
不要なビットを変更したり削除したりすることによって、私はこれを完全に定義して呼び出してもらうことができます。 – Nick