结果#encoding = utf-8
#题目:
#生成一个n级深度的字典,例如:[1,2,3,4,5,6] 可以生成{1: {2: {3: {4: {6: 5}}}}},写一个函数定义n级
a=[1,2,3,4,5,6]
def fun(n,i=0,dict1={}):
if i ==n-1:
dict1[a[i]] = a[i+1:]
if i < n-1:
dict1[a[i]] = fun(n,i+1,dict1={})
#print "dict1:",dict1
return dict1
print "a:",a
print fun(2)
print fun(4)
结果:
原文地址:https://www.cnblogs.com/xiaxiaoxu/p/10247254.html
时间: 2024-10-13 13:55:23