用重复值平整字典

时间:2017-10-04 16:10:25

标签: python list dictionary list-comprehension flatten

寻找没有循环的最有效方式...... 给出下面的Python(2.7)字典:

l = [{
"General": {
    "Iteration": {
        "CATID": 74470
    }
},
"Return": {
    "Effectiveness": {
        "Metrics": [{
            "Volume": 1004287.8947531971,
            "BusinessMetricID": 1,
            "ImpactScore": 0.032579772546071015,
            "GrossMargin": 10042878.94753197,
            "Revenue": 20085757.89506394
        },
        {
            "Volume": 2678101.0526751927,
            "BusinessMetricID": 2,
            "ImpactScore": 0.08687939345618939,
            "GrossMargin": 6695252.631687982,
            "Revenue": 13390505.263375964
        }]
    }
}
},
{
"General": {
    "Iteration": {
        "CATID": 74471
    }
},
"Return": {
    "Effectiveness": {
        "Metrics": [{
            "Volume": 1004287.8947531971,
            "BusinessMetricID": 1,
            "ImpactScore": 0.032579772546071015,
            "GrossMargin": 10042878.94753197,
            "Revenue": 20085757.89506394
        },
        {
            "Volume": 2678101.0526751927,
            "BusinessMetricID": 2,
            "ImpactScore": 0.08687939345618939,
            "GrossMargin": 6695252.631687982,
            "Revenue": 13390505.263375964
        }]
    }
}
}]

我试图在添加CATID属性时展平列表的度量列表,以获取以下词典列表

[{
"CATID": 74470,    
"Volume": 35399.19921217802,
"BusinessMetricID": 1,
"ImpactScore": 0.015,
"GrossMargin": 353991.9921217802,
"Revenue": 707983.9842435603
},
{
"CATID": 74470,
"Volume": 94397.86456580806,
"BusinessMetricID": 2,
"ImpactScore": 0.04,
"GrossMargin": 235994.66141452017,
"Revenue": 471989.32282904035
},
{
"CATID": 74471,    
"Volume": 35399.19921217802,
"BusinessMetricID": 1,
"ImpactScore": 0.015,
"GrossMargin": 353991.9921217802,
"Revenue": 707983.9842435603
},
{
"CATID": 74471,
"Volume": 94397.86456580806,
"BusinessMetricID": 2,
"ImpactScore": 0.04,
"GrossMargin": 235994.66141452017,
"Revenue": 471989.32282904035
}
]

最有效的方法是什么?我试图使用列表理解,但我找不到添加额外属性的方法。 简单地展平我使用过的指标:

[item for sublist in [x['Return']['Effectiveness']['Metrics'] for x in l] for item in sublist]

给了我:

[{
'Volume': 1004287.8947531971,
'BusinessMetricID': 1,
'ImpactScore': 0.032579772546071015,
'GrossMargin': 10042878.94753197,
'Revenue': 20085757.89506394
 },
{
'Volume': 2678101.0526751927,
'BusinessMetricID': 2,
'ImpactScore': 0.08687939345618939,
'GrossMargin': 6695252.631687982,
'Revenue': 13390505.263375964
 },
 {
'Volume': 1004287.8947531971,
'BusinessMetricID': 1,
'ImpactScore': 0.032579772546071015,
'GrossMargin': 10042878.94753197,
'Revenue': 20085757.89506394
 },
 {
'Volume': 2678101.0526751927,
'BusinessMetricID': 2,
'ImpactScore': 0.08687939345618939,
'GrossMargin': 6695252.631687982,
'Revenue': 13390505.263375964
 }]

谢谢。

1 个答案:

答案 0 :(得分:1)

我认为专家可以提供更好的解决方案。 这是我的解决方案。你可以留意itertools模块。

M[7,,] = m2[4,,]

输出:

from itertools import product, chain
nl = chain.from_iterable([ product( i['General']['Iteration'].items(), [ j.items() for j in i['Return']['Effectiveness']['Metrics'] ]) for i in l ])
[ dict([i] + list(j)) for i,j in nl ]