如何在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
.
.
.
感谢您的帮助
有点脆弱,因为它假设同一级别的所有元素都有子元素或没有子元素
输出(为可读性重新格式化)
相关问题 更多 >
编程相关推荐