0

我很难遍历 deps 中的每个值。目前只能循环一次。我在下面有预期的输出。任何人都可以提供有关如何更改为循环以获得下面的预期输出的输入吗?

'''
EXPECTED OUTPUT:-
external_dep = {
    '253036': ['12345','34567'],
    '12345': [],
    '34567': ['99999']
    '99999': []
    }
'''
def  findexternaldep(num):
    if num == '253036':
        output =['12345','34567']
    if num == '12345':
        output = []
    if num == '34567':
        output = ['99999']
    if num == '99999':
        output = []
    return output

master_gerrit = ["253036"]
external_dep={}
for num in master_gerrit:
    deps=findexternaldep(num)
    #print deps
    external_dep[num] = deps
    #loop should be repeated for each value in the list deps
print external_dep
4

1 回答 1

1

目前没有递归。您需要一个递归调用的函数,而不是循环。像这样的东西:

external_dep={}
def handle_gerrit(num, external_dep):
    deps=findexternaldep(num)
    external_dep[num] = deps
    for num in deps:
        handle_gerrit(num, external_dep)

handle_gerrit("253036", external_dep)
print external_dep
于 2013-04-29T04:24:49.400 回答