返回嵌套字典中最大深度处的键值

2024-09-30 02:23:07 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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. 返回键1的值
  2. 将分数1和分数2的值相加
  3. 返回键2到达时的值 每个分支的最大深度

结果如下:

{"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呢?你知道吗


Tags: keynonechaininput字典key2key1depth
1条回答
网友
1楼 · 发布于 2024-09-30 02:23:07

试试这个:

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'
}

相关问题 更多 >

    热门问题