我有不固定顺序的python嵌套字典,并根据子计数和大小值更新父计数和大小值。但是这两个值在最后一个嵌套字典中都可用。
输入字典-
[{'name': 'stack', 'children':
[{'name': 'flow', 'children':
[{'name': 'lldp', 'children':
[{'name': 'sourc', 'children':
[{'name': 'lldque.jrc', 'count': '11', 'size': '37'}]}]},
{'name': 'arp', 'children':
[{'name': 'src', 'children':
[{'name': 'arpred.cec', 'count': '37', 'size': '67'}]}]}]}]}]
输出字典应如下-
[{'name':'stack','count':'4','size':'6','children':
[{'name':'flow','count':'4','size':'6','children':
[{'name':'lldp','count':'1','size':'2','children':
[{'name':'sourc','count':'1','size':'2','children':
[{'name':'lldque.jrc','count':'1','size':'2'}]}]},
{'name':'arp','count':'3','size':'4','children':
[{'name':'src','count':'3','size':'4','children':
[{'name':'arpred.cec','count':'3','size':'4'}]}]}]}]}]
当前我正在使用以下代码;
def populateParentDict(ddict):
listl = []
if type(ddict) == list:
if 'children' in ddict[0]:
return populateParentDict(ddict[0])
else:
listl.append(ddict[0]['Buggy'])
listl.append(ddict[0]['Clean'])
elif 'children' in ddict:
return populateParentDict(ddict['children'])
return listl