Jul-09-2018, 06:45 AM
Hello,
I am writing a recursive function for dictionary of variable depth. I've posted a sample of this dictionary below. Only 'call_next_rule' key can have a nested dictionary as its value.
(I do realize I am not returning a list here, but rather nested tuples)
1. For a 2 level deep nested dictionary, uncommenting above two lines and then returning 'result' list back works. But it fails for 3 level or deeper dictionary.
2. I have used a 'static' counter variable to count number of times 'get_edges()' is called. Then I use this counter in another function to get the output that I want. I feel that this is clumsy.
What should I do here to get the result I want?
P.S. Last time I worked with recursion was over 10 years ago with C++ and tree algorithms. I barely did it then, and I am barely doing it now. Recursion is not my strong point and I am a novice in Python.
I have tried avoiding recursion but 'stack of iterators' pattern is confusing to me and I just don't know how to break this problem into multiple 'while' or 'for' loops. I am open to any suggestions that you may have.
(Thanks in advance)
I am writing a recursive function for dictionary of variable depth. I've posted a sample of this dictionary below. Only 'call_next_rule' key can have a nested dictionary as its value.
d4 = {'call_next_rule': {'call_next_rule': {'call_next_rule': None, 'executed_rule': 'rule_4B', 'rule_successful': True}, 'executed_rule': 'rule_4A', 'rule_successful': True}, 'executed_rule': 'rule_4', 'rule_successful': True}The desired output is:
Output:[('MyRoot', 'rule_4'), ('rule_4', 'rule_4A'), ('rule_4A', 'rule_4B')]
Instead I get nested tuples (I do realize I am not returning a list here, but rather nested tuples)
Output:('My Root', ('rule_4', ('rule_4A', 'rule_4B')))
Here is my recursive function:def get_edges(d, parent = None): result = list() if parent is None: parent = 'My Root' for k,v in d.items(): if d['call_next_rule'] == None: #base case child = d['executed_rule'] return (parent, child) else: if isinstance(v, dict): child = get_edges(v, parent = d['executed_rule']) #result.append((parent, child[0])) #result.append((child[0], child[1])) return (parent, child) get_edges(d4)I've tried 2 solutions to get the desired output that I want:
1. For a 2 level deep nested dictionary, uncommenting above two lines and then returning 'result' list back works. But it fails for 3 level or deeper dictionary.
2. I have used a 'static' counter variable to count number of times 'get_edges()' is called. Then I use this counter in another function to get the output that I want. I feel that this is clumsy.
What should I do here to get the result I want?
P.S. Last time I worked with recursion was over 10 years ago with C++ and tree algorithms. I barely did it then, and I am barely doing it now. Recursion is not my strong point and I am a novice in Python.
I have tried avoiding recursion but 'stack of iterators' pattern is confusing to me and I just don't know how to break this problem into multiple 'while' or 'for' loops. I am open to any suggestions that you may have.
(Thanks in advance)