Issue
I have a json file like below:
{
"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"
]
}
}]
}
}
]
}
}
]
}
}
From this, I want to find all walks of each node. By using the following code, I somehow managed to get the result given below.
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
Intermediate result:
print(edges)
[('App Builder', 'API Builder'), ('App Builder', 'UI'), ('UI', 'UI Builder'), ('UI', 'Template'), ('Template', 'Angular'), ('Template', 'React'), ('Template', 'PHP')]
Now I want to find the path of each nodes. i.e.,
['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']
How can I get these values?
Can I get this path from edges
only by converting the list into tree?
Any other better approach for this problem?
any help would be appreciable.
Solution
You want to generate a list of strings, that represent the path to each node connected through "children"
to other nodes, with the path being made up of the keys of the nodes.
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))))
Note that paths()
is a generator, yielding one result at a time, which is why the result of paths()
is wrapped in list()
before printing the result.
Answered By - Grismar
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.