0
点赞
收藏
分享

微信扫一扫

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

小磊z 2023-01-13 阅读 108


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


举报

相关推荐

0 条评论