2013-08-05 48 views
3

我有兩個類型的字典是這樣,Python的字典此外

past = 
{ 
    '500188': 
    { 
     2: {'S': 16.97011741552128, 'C': 16.97011741552128}, 
     3: {'S': -41.264072314989576, 'C': 'ERROR: reported_eps value not found for the year 2012.'}, 
     4: {'S': -40.45410186823402, 'C': 'ERROR: reported_eps value not found for the year 2012.'} 
    }, 
    '524715': 
    { 
     2: {'S': 46.21665549733925, 'C': 38.67504905630727}, 
     3: {'S': -32.729615295373385, 'C': -34.21172523465267}, 
     4: {'S': -22.25028773515787, 'C': -36.041635048402} 
    }, 
    '513683': 
    { 
     2: {'S': 6.319158390481139, 'C': 6.319158390481139}, 
     3: {'S': 19.81072942574542, 'C': 19.81072942574542}, 
     4: {'S': 6.367182731764687, 'C': 'ERROR: reported_eps value not found for the year 2008.'} 
    } 
} 

future = 
{ 
    '500188': 
    { 
     2: {'S': 16.97011741552128, 'C': 16.97011741552128}, 
     3: {'S': -41.264072314989576, 'C': 'ERROR: reported_eps value not found for the year 2012.'}, 
     4: {'S': -40.45410186823402, 'C': 'ERROR: reported_eps value not found for the year 2012.'} 
    }, 
    '524715': 
    { 
     2: {'S': 46.21665549733925, 'C': 38.67504905630727}, 
     3: {'S': -32.729615295373385, 'C': -34.21172523465267}, 
     4: {'S': -22.25028773515787, 'C': -36.041635048402} 
    } 
} 

加入他們,我已經做到了這一點,

def _float(value): 
    try: 
     return float(value) 
    except ValueError: 
     return 0 


print {key: 
    { 
     year: { 
       _type: 
       (_float(past.get(key, {}).get(year, {}).get(_type, 0)) + _float(future.get(key, {}).get(year, {}).get(_type, 0)))/2 for _type in ['S', 'C'] 

     }for year in [4,3,2] #Second Loop 

    }for key in set(past.keys()+future.keys()) #First Loop 
} 

得到所需的輸出,

{ 
    '500188': 
    { 
     2: {'S': 16.97011741552128, 'C': 16.97011741552128}, 
     3: {'S': -41.264072314989576, 'C': 0}, 
     4: {'S': -40.45410186823402, 'C': 0} 
    }, 
    '513683': 
    { 
     2: {'S': 3.1595791952405694, 'C': 3.1595791952405694}, 
     3: {'S': 9.90536471287271, 'C': 9.90536471287271}, 
     4: {'S': 3.1835913658823434, 'C': 0.0} 
    }, 
    '524715': 
    { 
     2: {'S': 46.21665549733925, 'C': 38.67504905630727}, 
     3: {'S': -32.729615295373385, 'C': -34.21172523465267}, 
     4: {'S': -22.25028773515787, 'C': -36.041635048402} 
    } 
} 

但是,有應該是比這個更好的解決方案,我搜索了一下,發現了這個類似的問題,

"Adding" Dictionaries in Python?

python dict.add_by_value(dict_2)?

但值是頂級水平,但對我來說價值不是頂層,我必須做一個類型檢查,並要計算平均值。解決這個問題的最好方法是什麼?

(Python版本2.7)

回答

1

與階層數據作爲嵌套的類型的字典工作總是將是痛苦;你最好使用庫如Pandas

import pandas as pd, numpy as np 
pp = pd.Panel(past).to_frame() 
pf = pd.Panel(future).to_frame() 
pp.replace('.', 0, regex=True) + pf.replace('.', 0, regex=True)/2 

       500188 513683  524715 
major minor        
C  2  25.455176  NaN 58.012574 
     3  0.000000  NaN -51.317588 
     4  0.000000  NaN -54.062453 
S  2  25.455176  NaN 69.324983 
     3  -61.896108  NaN -49.094423 
     4  -60.681153  NaN -33.375432 
+0

真的很好的解決方案,謝謝。我必須學習熊貓:-)。所以,如果我們要使用嵌套字典,最好使用像庫這樣的Pandas。我對麼 ? –

+2

@JohnPrawyn是的,絕對。 – ecatmur