以递归方式循环遍历deps中的每个值

时间:2013-04-29 03:45:05

标签: python

我很难在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

1 个答案:

答案 0 :(得分: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