多个嵌套字典遵循层次结构
Multiple nested dictionaries following hierarchy
我已经苦苦挣扎了一段时间,试图弄清楚如何将平面 table 中的一些层次值放入特定的字典格式中。主要问题是我不知道如何将每个类别嵌套在相应的键中。
我有这个 table(作为 pandas DataFrame),其中列以数字表示层次结构:
table 有三列:
Level Name Description
0 Main ...
1 Sub main ...
2 Sub sub main ...
1 Sub main ...
2 Sub sub main ...
3 Sub sub sub main ...
0 Main_2 ...
. . .
预期的输出应该是这样的:
{
"nodes": [
{
"name": "main",
"description": "",
"owners":{
"users":["Sandra"]
},
"terms":[{
"name":"",
"description":""
}]
},
{
"nodes": [
{
"name": "sub_main",
"description": "",
"owners":{
"users":[""]
},
"terms":[{
"name":"",
"description":"",
"inherits":[""]
}]
},
{
"nodes": [
{
"name": "sub_sub_main",
"description": "",
"owners":{
"users":[""]
},
"terms":[{
"name":"",
"description":"",
"inherits":[""]
}]
},
]
}
]
}
]
}
我有一个大的 table 具有多个层次。有时它只有 2 或 3 个级别,而在其他情况下,更多。但是,一切都井井有条。
还有一点就是在inherits
部分,上面肯定出现了parents。
我正在尝试构建一个递归函数,但到目前为止我都失败了。我检查了这些其他类似的问题:
- Access nested dictionary items via a list of keys?
有没有人知道类似这种方法的问题?或者你们是否遇到过类似的问题?
提前谢谢大家!
鉴于此源数据框:
df = pd.DataFrame.from_dict( {'Level': {0: 1, 1: 2, 2: 3, 3: 2, 4: 2, 5: 3, 6: 4, 7: 1},
'Name': {0: 'Main 1',
1: 'Sub main 1.1',
2: 'Sub sub main 1.1.1',
3: 'Sub main 1.2(a)',
4: 'Sub main 1.2(b)',
5: 'Sub sub main 1.2.1',
6: 'Sub sub sub main 1.2.1.1',
7: 'Main 2'},
'Description': {0: 'Sandra',
1: 'Andrew',
2: 'Sally',
3: 'Mark',
4: 'Simon',
5: 'Sinead',
6: 'Holly',
7: 'Max'}})
我们可以构建一个树形字典,希望它接近您想要的:
tree = {
'children': [],
'ancestors': [],
'parent': None,
'Level': 0,
'Name': 'root'
}
curr_node = tree
for index, row in df.iterrows():
# new child node
if row.Level > curr_node['Level']:
parent = curr_node
ancestors = parent['ancestors'] + [parent['Name']]
# sibling node
elif row.Level == curr_node['Level']:
parent = curr_node['parent']
ancestors = curr_node['ancestors'].copy()
# ...or skip back up the hierarchy
elif row.Level < curr_node['Level']:
# skipping up until curr_node is the proper parent for this level
while curr_node['Level'] >= row.Level:
curr_node = curr_node['parent']
parent = curr_node
ancestors = curr_node['ancestors'] + [parent['Name']]
# make new node with given parent & ancestors
curr_node = row.to_dict()
curr_node['children'] = []
curr_node['parent'] = parent
curr_node['ancestors'] = ancestors
parent['children'].append(curr_node)
结果:
from pprint import pprint
pprint(tree)
{'Name': 'root',
'ancestors': [],
'children': [{'Description': 'Sandra',
'Level': 1,
'Name': 'Main 1',
'ancestors': ['root'],
'children': [{'Description': 'Andrew',
'Level': 2,
'Name': 'Sub main 1.1',
'ancestors': ['root', 'Main 1'],
'children': [{'Description': 'Sally',
'Level': 3,
'Name': 'Sub sub main 1.1.1',
'ancestors': ['root',
'Main 1',
'Sub main 1.1'],
'children': [],
'level': 3,
'parent': <Recursion on dict with id=140209092851264>}],
'level': 2,
'parent': <Recursion on dict with id=140209092850624>},
{'Description': 'Mark',
'Level': 2,
'Name': 'Sub main 1.2(a)',
'ancestors': ['root', 'Main 1'],
'children': [],
'level': 2,
'parent': <Recursion on dict with id=140209092850624>},
{'Description': 'Simon',
'Level': 2,
'Name': 'Sub main 1.2(b)',
'ancestors': ['root', 'Main 1'],
'children': [{'Description': 'Sinead',
'Level': 3,
'Name': 'Sub sub main 1.2.1',
'ancestors': ['root',
'Main 1',
'Sub main 1.2(b)'],
'children': [{'Description': 'Holly',
'Level': 4,
'Name': 'Sub sub sub '
'main 1.2.1.1',
'ancestors': ['root',
'Main 1',
'Sub '
'main '
'1.2(b)',
'Sub '
'sub '
'main '
'1.2.1'],
'children': [],
'level': 4,
'parent': <Recursion on dict with id=140209092851520>}],
'level': 3,
'parent': <Recursion on dict with id=140209092851456>}],
'level': 2,
'parent': <Recursion on dict with id=140209092850624>}],
'level': 1,
'parent': <Recursion on dict with id=140209075210048>},
{'Description': 'Max',
'Level': 1,
'Name': 'Main 2',
'ancestors': ['root'],
'children': [],
'level': 1,
'parent': <Recursion on dict with id=140209075210048>}],
'level': 0,
'parent': None}
我已经苦苦挣扎了一段时间,试图弄清楚如何将平面 table 中的一些层次值放入特定的字典格式中。主要问题是我不知道如何将每个类别嵌套在相应的键中。
我有这个 table(作为 pandas DataFrame),其中列以数字表示层次结构: table 有三列:
Level Name Description
0 Main ...
1 Sub main ...
2 Sub sub main ...
1 Sub main ...
2 Sub sub main ...
3 Sub sub sub main ...
0 Main_2 ...
. . .
预期的输出应该是这样的:
{
"nodes": [
{
"name": "main",
"description": "",
"owners":{
"users":["Sandra"]
},
"terms":[{
"name":"",
"description":""
}]
},
{
"nodes": [
{
"name": "sub_main",
"description": "",
"owners":{
"users":[""]
},
"terms":[{
"name":"",
"description":"",
"inherits":[""]
}]
},
{
"nodes": [
{
"name": "sub_sub_main",
"description": "",
"owners":{
"users":[""]
},
"terms":[{
"name":"",
"description":"",
"inherits":[""]
}]
},
]
}
]
}
]
}
我有一个大的 table 具有多个层次。有时它只有 2 或 3 个级别,而在其他情况下,更多。但是,一切都井井有条。
还有一点就是在inherits
部分,上面肯定出现了parents。
我正在尝试构建一个递归函数,但到目前为止我都失败了。我检查了这些其他类似的问题:
- Access nested dictionary items via a list of keys?
有没有人知道类似这种方法的问题?或者你们是否遇到过类似的问题?
提前谢谢大家!
鉴于此源数据框:
df = pd.DataFrame.from_dict( {'Level': {0: 1, 1: 2, 2: 3, 3: 2, 4: 2, 5: 3, 6: 4, 7: 1},
'Name': {0: 'Main 1',
1: 'Sub main 1.1',
2: 'Sub sub main 1.1.1',
3: 'Sub main 1.2(a)',
4: 'Sub main 1.2(b)',
5: 'Sub sub main 1.2.1',
6: 'Sub sub sub main 1.2.1.1',
7: 'Main 2'},
'Description': {0: 'Sandra',
1: 'Andrew',
2: 'Sally',
3: 'Mark',
4: 'Simon',
5: 'Sinead',
6: 'Holly',
7: 'Max'}})
我们可以构建一个树形字典,希望它接近您想要的:
tree = {
'children': [],
'ancestors': [],
'parent': None,
'Level': 0,
'Name': 'root'
}
curr_node = tree
for index, row in df.iterrows():
# new child node
if row.Level > curr_node['Level']:
parent = curr_node
ancestors = parent['ancestors'] + [parent['Name']]
# sibling node
elif row.Level == curr_node['Level']:
parent = curr_node['parent']
ancestors = curr_node['ancestors'].copy()
# ...or skip back up the hierarchy
elif row.Level < curr_node['Level']:
# skipping up until curr_node is the proper parent for this level
while curr_node['Level'] >= row.Level:
curr_node = curr_node['parent']
parent = curr_node
ancestors = curr_node['ancestors'] + [parent['Name']]
# make new node with given parent & ancestors
curr_node = row.to_dict()
curr_node['children'] = []
curr_node['parent'] = parent
curr_node['ancestors'] = ancestors
parent['children'].append(curr_node)
结果:
from pprint import pprint
pprint(tree)
{'Name': 'root',
'ancestors': [],
'children': [{'Description': 'Sandra',
'Level': 1,
'Name': 'Main 1',
'ancestors': ['root'],
'children': [{'Description': 'Andrew',
'Level': 2,
'Name': 'Sub main 1.1',
'ancestors': ['root', 'Main 1'],
'children': [{'Description': 'Sally',
'Level': 3,
'Name': 'Sub sub main 1.1.1',
'ancestors': ['root',
'Main 1',
'Sub main 1.1'],
'children': [],
'level': 3,
'parent': <Recursion on dict with id=140209092851264>}],
'level': 2,
'parent': <Recursion on dict with id=140209092850624>},
{'Description': 'Mark',
'Level': 2,
'Name': 'Sub main 1.2(a)',
'ancestors': ['root', 'Main 1'],
'children': [],
'level': 2,
'parent': <Recursion on dict with id=140209092850624>},
{'Description': 'Simon',
'Level': 2,
'Name': 'Sub main 1.2(b)',
'ancestors': ['root', 'Main 1'],
'children': [{'Description': 'Sinead',
'Level': 3,
'Name': 'Sub sub main 1.2.1',
'ancestors': ['root',
'Main 1',
'Sub main 1.2(b)'],
'children': [{'Description': 'Holly',
'Level': 4,
'Name': 'Sub sub sub '
'main 1.2.1.1',
'ancestors': ['root',
'Main 1',
'Sub '
'main '
'1.2(b)',
'Sub '
'sub '
'main '
'1.2.1'],
'children': [],
'level': 4,
'parent': <Recursion on dict with id=140209092851520>}],
'level': 3,
'parent': <Recursion on dict with id=140209092851456>}],
'level': 2,
'parent': <Recursion on dict with id=140209092850624>}],
'level': 1,
'parent': <Recursion on dict with id=140209075210048>},
{'Description': 'Max',
'Level': 1,
'Name': 'Main 2',
'ancestors': ['root'],
'children': [],
'level': 1,
'parent': <Recursion on dict with id=140209075210048>}],
'level': 0,
'parent': None}