在嵌套字典中以最大深度返回键值

时间:2018-06-21 13:10:04

标签: python dictionary nested max

我有一个嵌套的字典,如下所示:

d = {'chain': [{'chain': [{'chain': [None, None, None, None, None],
     'depth': 2,
     'key1': 'A11',
     'key2': 'B11',
     'score1': 0.3,
     'score2': 0.6},
    {'chain': [{'chain': [None, None, None, None, None],
       'depth': 3,
       'key1': 'A121',
       'key2': 'B121',
       'score1': 0.2,
       'score2': 0.6}],
     'depth': 2,
     'key1': 'A12',
     'key2': 'B12',
     'score1': 0.5,
     'score2': 0.7}],
   'depth': 1,
   'key1': 'A1',
   'key2': 'B1',
   'score1': 0.2,
   'score2': 0.5},
  {'chain': [{'chain': [None, None, None, None, None],
     'depth': 2,
     'key1': 'A22',
     'key2': 'B22',
     'score1': 0.1,
     'score2': 0.5}],
   'depth': 1,
   'key1': 'A2',
   'key2': 'B2',
   'score1': 0.1,
   'score2': 0.2}],
 'depth': 0,
 'key1': 'A',
 'key2': 'B',
 'score1': 0.1,
 'score2': 0.4}

我想创建一个函数,当我调用fun(key1, d)时,它可以返回一个保留原始层次结构的字典,但是在每个级别中,它将:

  1. 返回key1的值
  2. 总结得分1和得分2的值
  3. 在到达key2时返回其值 每个分支的最大深度

结果如下:

{"A":0.5, "depth":0, "chain":[
{"A1":0.7, "depth":1,"chain":[
    {"A11":0.9,"depth":2,"key2":"B11", "chain":[]}, 
    {"A12":1.3, "depth":2,"chain":[
        {"A121":0.8, "depth":3, "key2":"B121", "chain":[]}
    ]}]},
 {"A2":0.3,"depth":1,"chain":[
     {"A22":0.6, "depth":2, "key2":"B22","chain":[]}
 ]}]

}

我使用以下方法获得了1和2:

def gen_dict_extract(key, input_dic):
rv = {
    input_dic[key]: input_dic["score1"] + input_dic["score2"],
    "depth": input_dic["depth"],
}
if "chain" in input_dic:
    rv["chain"]=[]
    for x in input_dic["chain"]:
        if x is not None:
            rv["chain"].insert(input_dic["chain"].index(x),gen_dict_extract(key, x))
return rv

但是我怎么加3?

1 个答案:

答案 0 :(得分:1)

尝试一下:

def get_values_with_depth(key, d):
  result = [(d[key], d['depth'])]
  for c in d['chain']:
    if c is not None:
      result.extend(get_values_with_depth(key, c))
  return result

def gen_dict_extract(key1, key2, d):
  return {
    d[key1]: d['score1'] + d['score2'],
    'depth': d['depth'],
    'chain': [gen_dict_extract(key1, key2, c) for c in d['chain'] if c is not None],
    key2: max(get_values_with_depth(key2, d), key=lambda x: x[1])[0]
  }

print(gen_dict_extract('key1', 'key2', data))

它打印:

{
    'A': 0.5,
    'depth': 0,
    'chain': [{
        'A1': 0.7,
        'depth': 1,
        'chain': [{
            'A11': 0.8999999999999999,
            'depth': 2,
            'chain': [],
            'key2': 'B11'
        }, {
            'A12': 1.2,
            'depth': 2,
            'chain': [{
                'A121': 0.8,
                'depth': 3,
                'chain': [],
                'key2': 'B121'
            }],
            'key2': 'B121'
        }],
        'key2': 'B121'
    }, {
        'A2': 0.30000000000000004,
        'depth': 1,
        'chain': [{
            'A22': 0.6,
            'depth': 2,
            'chain': [],
            'key2': 'B22'
        }],
        'key2': 'B22'
    }],
    'key2': 'B121'
}