我有描述树(父-子)的数据。我想在此基础上构建一个JSON文件,并将父级的子级分配给树中找到父级的任何位置。你知道吗
这个问题是这个答案的延伸: https://stackoverflow.com/a/57216064/1233240
代码:
file = [
('parent1', ['child1', 'child2', 'child3']),
('parent2', ['child4', 'child5', 'child6']),
('child1', ['child7', 'child8']),
('child5', ['child10', 'child33']),
('parent3', ['child1', 'child2', 'child3'])
]
json_dict = {}
flat_dict = {}
for parent, children in file:
if parent in flat_dict:
value = flat_dict[parent]
else:
value = {}
flat_dict[parent] = json_dict[parent] = value
for child in children:
flat_dict[child] = value[child] = {}
电流输出:
{
"parent1": {
"child1": {
"child7": {},
"child8": {}
},
"child2": {},
"child3": {}
},
"child1": {},
"child2": {},
"child3": {},
"parent2": {
"child4": {},
"child5": {
"child10": {},
"child33": {}
},
"child6": {}
},
"child4": {},
"child5": {
"child10": {},
"child33": {}
},
"child6": {},
"child7": {},
"child8": {},
"child10": {},
"child33": {},
"parent3": {
"child1": {},
"child2": {},
"child3": {}
}
}
预期输出:
我希望所有child1
节点如下:
"child1": {
"child7": {},
"child8": {}
}
但是我们可以看到,只有在parent1
节点中它才保留它的子节点。你知道吗
一种方法是对链接的答案添加一个简单的修改,检查子对象是否已经存在,并使用现有的对象。你知道吗
在这两种情况下,都可以使用^{} 来缩短代码,但我通常不会使用它,因为它会创建一个空的dictionary实例,即使这不是必需的:
虽然第二个解决方案执行一些可能不必要的操作,但代码看起来确实更加清晰。你知道吗
同样干净但不必要对象较少的另一种方法是使用^{} :
除了需要导入外,这个解决方案不会像基于
dict
的常规解决方案那样清晰地打印。但是,这不会以任何方式影响到到到JSON的转换。你知道吗由于您注释说实际上不希望子节点位于根中,因此此处显示的代码段将仅将父节点放置在根中。但是,如果您稍后发现某个节点是子节点,则它们不会从根节点中删除该节点。这可以通过另一个简单的修改来实现,如
defaultdict
示例所示,但也适用于所有其他示例:这里有一个IDEOne link和最后的答案。你知道吗
请记住,此代码不会检查循环引用。循环引用将自动从结果中删除自身。例如:
希望这是你想要的。你知道吗
代码:
输出:
相关问题 更多 >
编程相关推荐