gpt4 book ai didi

python - 如何在python中将json转换为树

转载 作者:行者123 更新时间:2023-12-01 00:36:43 31 4
gpt4 key购买 nike

我有一个如下所示的 json 文件:

{
"App Builder": {
"utterance": [
"create an app",
"create app for me",
"can you create an application?"
],
"question": [
"Do you want to create application through UI or API Builder?",
"Do you want to try our getting started page?"
],
"children": [{
"API Builder": {
"utterance": [
"create an app using API Buider",
"make an application using API Builder",
"create API Builder application"
]
}
},
{
"UI": {
"utterance": [
"create an app using user interface",
"make an application using UI",
"create UI application"
],
"question": [
"Do you want to create application through Template or UI Builder?",
"Do you want to try our getting started page?"
]

,
"children": [{
"UI Builder": {
"utterance": [
"create an app using UI Buider",
"make an application using UI Builder",
"create UI Builder application"
]
}
},
{
"Template": {
"utterance": [
"create an app using Template",
"make an application using Template",
"create Template application"
],
"question": [
"Do you want to create application through Angular or React or PHP?",
"Do you want to try our getting started page?"
],
"children": [{
"Angular": {
"utterance": [
"create an app using Angular",
"make an application using Angular template",
"create Angular application"
]
}
}, {
"React": {
"utterance": [
"create an app using React",
"make an application using template React",
"create React application"
]
}
}, {
"PHP": {
"utterance": [
"create an app using PHP",
"make an application using template PHP",
"create PHP application"
]
}
}]
}
}
]
}
}
]
}
}

由此,我想找到每个节点的所有路径。通过使用以下代码,我以某种方式设法获得了下面给出的结果。

edges = []
leaves = []
nodes = []
def get_edges(treedict, parent=None):
try:
name = next(iter(treedict.keys()))
nodes.append(name)
if parent is not None:
edges.append((parent, name))
for item in treedict[name]["children"]:
if isinstance(item, dict):
get_edges(item, parent=name)
else:
edges.append((name, item))

except KeyError as e:
leaves.append(name)
pass

中间结果:

print(edges)

[('App Builder', 'API Builder'), ('App Builder', 'UI'), ('UI', 'UI Builder'), ('UI', 'Template'), ('Template', 'Angular'), ('Template', 'React'), ('Template', 'PHP')]

现在我想找到每个节点的路径。即,

['App Builder', 'App Builder/API Builder', 'App Builder/UI', 'App Builder/UI/UI Builder', 'App Builder/UI/Template',
'App Builder/UI/Template/Angular', 'App Builder/UI/Template/React', 'App Builder/UI/Template/PHP']

如何获取这些值?

我可以仅通过将列表转换为树来从edges获取这条路径吗?

还有其他更好的方法来解决这个问题吗?

任何帮助将不胜感激。

最佳答案

您想要生成一个字符串列表,该列表表示通过“children”连接到其他节点的每个节点的路径,该路径由节点的键组成。

import json


def paths(data):
for key, value in data.items():
yield key
if 'children' in value:
for child in value['children']:
for path in paths(child):
yield f'{key}/{path}'


with open('your_data.json') as f:
print(list(paths(json.load(f))))

请注意,paths() 是一个生成器,一次生成一个结果,这就是为什么 paths() 的结果被包装在 list( ) 在打印结果之前。

关于python - 如何在python中将json转换为树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57685165/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com