pop
из текущего пути, когда вы закончили посещать узел set
посещенных узлов Пример реализации выше:
def get_source(root, dict_input):
# output list
path_list = []
# current path
cur_path = []
# visited set
visited = set()
# internal recursive helper function
def helper(node):
cur_path.append(node)
# normal node
if node in dict_input:
if not node in visited:
visited.add(node)
for child in dict_input[node]:
helper(child)
# else: cycle detected, raise an exception?
# leaf node
else:
# important: must be a copy of the current path
path_list.append(list(cur_path))
cur_path.pop()
# call this helper function on the root node
helper(root)
return path_list
Тест:
>>> dict_input = {'C411':['C052'],'C052':['C001','C002'], 'C001':['9001'], 'C002':['9002']}
>>> get_source('C411', dict_input)
[['C411', 'C052', 'C001', '9001'], ['C411', 'C052', 'C002', '9002']]
Насколько я вижу, это - лучший способ сделать так - и нет никакого способа изменить метаданные с pyPDF.