2016-04-13 4 views
0

私は少しこれを処理しようとしてきましたが、部分的にはなってきましたが、まだかなりはありません。私は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) 

答えて

0

schwobasegglの答えは本当に良いと短いですが、私が必要な場合関数形式のtは以下のようになります。

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 
+0

不要なビットを変更したり削除したりすることによって、私はこれを完全に定義して呼び出してもらうことができます。 – Nick

0

つのdict-理解と利用をビルトイン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} 
関連する問題