递归 算例一(求一个简单嵌套字典的深度)

c=[]

def dcc(dic):

    for key in dic.keys():
        print (key)     
        c.append(key)       
        vv=dic[key]
        #判断下一级是否还是字典,如果是字典继续递归
        if type(vv) == dict:
            #print (len(dic[key]))
            dcc(vv)
        else:         
            print (vv)
            print ('--------------')
            print(c)
            print(len(c))

def main():
    test_dict = {'a':{'b':{'c':1,}}}
    dcc(test_dict)

dd=main()

print('the depth of the dict is ',len(c))
a
b
c
1
--------------
['a', 'b', 'c']
3
the depth of the dict is  3

猜你喜欢

转载自blog.csdn.net/luoganttcc/article/details/80811080
今日推荐