格式化二维扩展词典

2024-05-13 05:27:27 发布

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

如何在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
    .
    .
    .

感谢您的帮助


Tags: ofnonechildtitlemanmsigtj727ftj634f
1条回答
网友
1楼 · 发布于 2024-05-13 05:27:27
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')
        ]
    }
}

相关问题 更多 >