问题
I have a huge depth dictionary that represents forest (many non-binary trees) which I want to process the forest and create a text file with all possible relations of the forest, e.g. given the dictionary:
{'a': {'b': {'c': {}, 'd': {}}, 'g': {}}}
the generated text file will look like:
a b c
a b d
a g
Note that the nested dictionary is big and iterating over it recursively is makes a memory run-time error.
What I tried doing is converting the dictionary into a list of lists recursively which yields a run-time error. The code:
def return_list(forest):
for ent in forest.keys():
lst = [new_ent] + grab_children(forest[ent])
yield lst
def grab_children(father):
local_list = []
for key, value in father.items():
local_list.append(new_key)
local_list.extend(grab_children(value))
return local_list
The error: "maximum recursion depth exceeded in comparison" RuntimeError
回答1:
Without recursion, with generator and trampoline (with writing to file):
data = {'a': {'b': {'c': {}, 'd': {}}, 'g': {}}}
def write_dict(d, s=(), f_out=None):
if len(d) == 0:
if f_out:
f_out.write(' '.join(s) + '\n')
return
for k, v in reversed(list(d.items())):
yield write_dict, v, s + (k, ), f_out
with open('data_out.txt', 'w') as f_out:
stack = [write_dict(data, f_out=f_out)]
while stack:
try:
v = next(stack[-1])
except StopIteration:
del stack[-1]
continue
stack.insert(-1, v[0](v[1], v[2], v[3]))
The file contains:
a b c
a b d
a g
回答2:
def l(d):
return '\n'.join(k + (i and ' ' + i) for k, v in d.items() for i in l(v).split('\n'))
print(l({'a': {'b': {'c': {}, 'd': {}}, 'g': {}}}))
This outputs:
a b c
a b d
a g
回答3:
A non-recursive approach:
d = {'a': {'b': {'c': {}, 'd': {}}, 'g': {}}}
p = q = []
while True:
for k, v in d.items():
if v:
q.append((v, p + [k]))
else:
print(' '.join(p + [k]))
if not q:
break
d, p = q.pop(0)
This outputs:
a g
a b c
a b d
来源:https://stackoverflow.com/questions/51500003/writing-nested-dictionary-forest-of-a-huge-depth-to-a-text-file