格式化二维扩展词典

时间:2016-06-24 02:09:23

标签: python recursion

如何在python中编写二维递归函数,我的输入看起来像这样:

{   'Tj634f': {   'child': ['Tj256f', 'Tj996f'],
                      'title': 'FSIT'},
    'Tj227f': {'child': None, 'title': 'MEGP'},
    'Tj727f': {'child': None, 'title': 'MSIG/OF'},
    'Tj738f': {'child': None, 'title': 'EDVAN'},
    'Tj279f': {'child': None, 'title': 'L-L3dary'},
    'Tj230f': {'child': None, 'title': 'Mgea-Prog'},
    'Tj256f': {   'child': [   'Tj227f',
                               'Tj727f',
                               'Tj738f',
                               'Tj279f',
                               'Tj230f'],
                  'title': 'man'},
    'Tj996f': {   'child': [   'Tj997f',
                               'Tj998f',
                               'Tj999f',
                               'Tj800f'],
                  'title': 'Aut'},
    'Tj997f': {'child': None, 'title': 'DCGP'},
    'Tj998f': {'child': None, 'title': 'DBGP/PF'},
    'Tj999f': {'child': None, 'title': 'E-xAN'},
    'Tj800f': {'child': None, 'title': 'L-L3dary'},
    'root': 'Tj634f'}

我的输出应该如下所示:

{    ('FSIT', 'Tj634f'): {  ('Aut', 'Tj996f') : [ ('DCGP','Tj997f'),
                                                  ('DBGP/PF','Tj998f'),
                                                  ('E-xAN','Tj999f'),
                                                  ('L-L3dary','Tj800f')],
                            ('man', 'Tj256f')": [ ('MEGP', 'Tj227f'),
                                                  ('MSIG/OF', 'Tj727f'),
                                                  ('EDVAN', 'Tj738f'),
                                                  ('L-L3dary', 'Tj279f'),
                                                  ('Mgea-Prog','Tj230f')]
                          }
}

然而,字典中的级别是

FSIT 
   - man
      -- MEGP
      -- MSIG/OF
   - aut
      -- DCGP
      -- DBGP/PF   

在两个维度中可变,一个轴的深度为FSIT --> man --> MEGP --> ...,也有一个广度

FSIT
  -man
  -aut
  -rlt
    .
    .
    .

帮助赞赏

1 个答案:

答案 0 :(得分:1)

dictionary = {
    'Tj634f': {'child': ['Tj256f', 'Tj996f'], 'title': 'FSIT'},
    'Tj227f': {'child': None, 'title': 'MEGP'},
    'Tj727f': {'child': None, 'title': 'MSIG/OF'},
    'Tj738f': {'child': None, 'title': 'EDVAN'},
    'Tj279f': {'child': None, 'title': 'L-L3dary'},
    'Tj230f': {'child': None, 'title': 'Mgea-Prog'},
    'Tj256f': {'child': ['Tj227f', 'Tj727f', 'Tj738f', 'Tj279f', 'Tj230f'], 'title': 'man'},
    'Tj996f': {'child': ['Tj997f', 'Tj998f', 'Tj999f', 'Tj800f'], 'title': 'Aut'},
    'Tj997f': {'child': None, 'title': 'DCGP'},
    'Tj998f': {'child': None, 'title': 'DBGP/PF'},
    'Tj999f': {'child': None, 'title': 'E-xAN'},
    'Tj800f': {'child': None, 'title': 'L-L3dary'},
    'root': 'Tj634f'
}

def reformat(dictionary, roots):
    value = None

    for root in roots:
        sub_dictionary = dictionary[root]

        if sub_dictionary['child']:
            if value is None:
                value = {}
            value[(sub_dictionary['title'], root)] = reformat(dictionary, sub_dictionary['child'])
        else:
            if value is None:
                value = []
            value.append((sub_dictionary['title'], root))

    return value


print(reformat(dictionary, [dictionary['root']]))

有点脆弱,因为它假设同一级别的所有元素都有孩子或者都没有孩子。

输出(为便于阅读而重新格式化)

{
('FSIT', 'Tj634f'):
    {
    ('man', 'Tj256f'):
        [
        ('MEGP', 'Tj227f'),
        ('MSIG/OF', 'Tj727f'),
        ('EDVAN', 'Tj738f'),
        ('L-L3dary', 'Tj279f'),
        ('Mgea-Prog', 'Tj230f')
        ],
    ('Aut', 'Tj996f'):
        [
        ('DCGP', 'Tj997f'),
        ('DBGP/PF', 'Tj998f'),
        ('E-xAN', 'Tj999f'),
        ('L-L3dary', 'Tj800f')
        ]
    }
}