【问题标题】:Return a key value at the maximum depth in a nested dictionary返回嵌套字典中最大深度处的键值
【发布时间】:2018-11-30 20:07:25
【问题描述】:

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

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. 将score1和score2的值相加
  3. 当key2到达时返回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?

【问题讨论】:

    标签: python dictionary nested max


    【解决方案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'
    }
    

    【讨论】:

      猜你喜欢
      • 2022-12-18
      • 2014-05-18
      • 2022-06-11
      • 2013-03-14
      • 2011-02-01
      • 2020-10-03
      • 2019-02-06
      • 2019-08-22
      • 1970-01-01
      相关资源
      最近更新 更多