I have a dictionary of the following, of unknown hierarchical depth:
dict_of_dicts = {'a': {'b': {'c': {}, 'd': {}, 'e': {}}}, 'f': {'g': {}}}
I found the following useful for learning how to recurse this, but I have had trouble modifying the code to get what I want, which is a list of all the paths from the top hierarchal level to the dead ends.
The desired output is:
list = ['a,b,c', 'a,b,d', 'a,b,e', 'f,g']
To start approaching this, I used a DFS approach:
hierarchy = []
for parent in dict_of_dicts:
recurse_dicts(concepts, parent, hierarchy)
def recurse_dicts(concepts, parent, hierarchy):
hierarchy.append(parent)
for child in concepts[parents]:
if len(recurse[node][child].keys()) > 0:
recurse_dicts(recurse[node], child, hierarchy)
else:
return
This resulted in:
hierarchy = ['a', 'b', 'c', 'd', 'e']
which is something, but not quite what I wanted.