Python 字典加法

标签 python python-2.7

我有两个这样的指令,

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)

最佳答案

将分层数据作为嵌套字典使用总是很痛苦;你最好使用像 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

关于Python 字典加法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18058210/

相关文章:

python - 从用户指定的目录导入 python 模块?

Python 2.7 BeautifulSoup,电子邮件抓取

Python 导入错误 : No module named resource

python - 如何将 pdf 文件中的两页合并为一页

python - BeautifulSoup,请求 - 以下代码有什么区别?

python - 创建/分发基于 Jupyter Notebook/Jupyter Lab 的独立应用程序的最佳方式?

python - 使用树莓派,Pi相机,Python和Open Cv进行人脸识别

python - 如何从不一致的字符串列表中获取子字符串?

python - django使用分页时出错

django - HTTP/1.0 301永久移动-Django