huavhuahua 2020-02-29
1.描述:
2.递归函数特性:
3.实例说明
#相加计算,递归方式 def fact(n): if n > 0: return fact(n-1) + n else: return 0 print(fact(5))
4.实例应用
#找出接口报文中某个字段的值 queryUserList = { "code": "200", "msg": "查询用户成功!", "model": { "user1": { "userAccount": "17779828882", "userName": "zhengying2", "userMobile": "17779828882", "userEmail": "" }, "user2": { "userAccount": "17779828881", "userName": "zhengying1", "userMobile": "17779828881", "userEmail": "" }, "user3": { "userAccount": "17779828880", "userName": "zhengying0", "userMobile": "17779828880", "userEmail": "" }, "user4": { "userAccount": "admin", "userName": "admin", "userMobile": "", "userEmail": "" }, "pages": 1 } } #代码如下 values = [] def get_value(getkey,dict1): if isinstance(dict1,dict): #验证入参格式是否为字典 for k,v in dict1.items(): #取出所以的键值对 if k == getkey: #判断所拿到的键是否是预期传入的键 values.append(v) get_value(getkey,v) #进行递归 get_value("userEmail",queryUserList) print(values)