[ Python ] 递归函数
一、 什么是递归函数
如果一个函数在内部调用自身本身,这个函数就是递归函数。
在递归函数中,是一个有去有回的过程。
二、递归函数的说明
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | def fact(n): if n = = 1 : return 1 return n * fact(n - 1 ) print (fact( 5 )) # fact(5) # ===> 5 * fact(4) # ===> 5 * (4 * fact(3)) # ===> 5 * (4 * (3 * fact(2))) # ===> 5 * (4 * (3 * (2 * fact(1)))) # ===> 5 * (4 * (3 * (2 * 1))) # ===> 5 * (4 * (3 * 2)) # ===> 5 * (4 * 6) # ===> 5 * 24 # ===> 120 |
通过上面的实例,可以总结出:
递归函数就是在函数内部调用自身本身,直到自身返回的是一个具体的值而不是函数,然后从内向外逐一返回函数的值,因此递归是一个有去有回两个过程组成的;
三、一个递归函数的实例
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
menu = { '陕西省': { '西安':{ '未央区':{}, '莲湖区':{}, '高新区':{}, }, '咸阳':{ '秦都区':{}, '渭城区':{} } }, '四川省': { '成都':{ '锦江区':{}, '青羊区':{}, '金牛区':{}, }, '绵阳':{ '涪城区': {}, '游仙区': {}, } } } def threeTL(dic): while True: for key in dic: print(key) choice = input('>>>').strip() if choice == 'b' or choice == 'q': return choice elif choice in dic and dic[choice]: res = threeTL(dic[choice]) if res == 'q': return 'q' elif not choice or choice not in dic: continue threeTL(menu)
该实例是选择省打印城市,输入城市打印区, 当用户输入 b 返回上一层,当用户输入 q 退出整个程序,这里最难理解的就是 b 和 q 的用法;
分析如下:
(1)正常递归深入过程:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
# def threeTL(dic): # while True: # for key in dic: print(key) # 陕西省、四川省 # choice = input('>>>').strip() # 用户输入:陕西省 # if choice == 'b' or choice == 'q': return choice # elif choice in dic and dic[choice]: # res = threeTL(dic[choice]) # 递归返回 threeTL(dic['陕西省']) # if res == 'q': return 'q' # elif not choice or choice not in dic: # continue # # def threeTL(dic): # dic = dic['陕西省'] # while True: # for key in dic: print(key) # 西安、咸阳 # choice = input('>>>').strip() # 用户输入:西安 # if choice == 'b' or choice == 'q': return choice # elif choice in dic and dic[choice]: # res = threeTL(dic[choice]) # 递归返回 threeTL(dic['陕西省']['西安']) # if res == 'q': return 'q' # elif not choice or choice not in dic: # continue # # def threeTL(dic): # dic = dic['陕西省']['西安'] # while True: # for key in dic: print(key) # 未央莲湖高新 # choice = input('>>>').strip() # # 用户输入:未央区 # if choice == 'b' or choice == 'q': return choice # elif choice in dic and dic[choice]: # res = threeTL(dic[choice]) # if res == 'q': return 'q' # elif not choice or choice not in dic: # 因为未央区没有值,进入下一次循环
1 2 3 4 | threeTL(menu) - - >threeTL(menu[ '陕西省' ]) - - >threeTL(menu[ '陕西省' ]) - - >threeTL(menu[ '陕西省' ][ '西安' ]) - - >threeTL(menu[ '陕西省' ]) - - >threeTL(menu[ '陕西省' ][ '西安' ]) - - >threeTL(menu[ '陕西省' ][ '西安' ][ '未央区' ]) |
(2)当用户输入 ' b ':
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
def threeTL(dic): # dic = dic['陕西省']['西安'] while True: for key in dic: print(key) # 未央莲湖高新 choice = input('>>>').strip() # # 用户输入:b if choice == 'b' or choice == 'q': return choice # 返回'b',程序退出 elif choice in dic and dic[choice]: res = threeTL(dic[choice]) if res == 'q': return 'q' elif not choice or choice not in dic: continue def threeTL(dic): # dic = dic['陕西省'] while True: for key in dic: print(key) # 第二次while循环打印 <西安、咸阳>(第一次while循环是在递归函数深入的时候执行) choice = input('>>>').strip() # 第二次while循环到此,用户输入 b if choice == 'b' or choice == 'q': return choice # 返回'b',程序退出 elif choice in dic and dic[choice]: res = threeTL(dic[choice]) # res = 'b', 本次if循环没有匹配到res退出,进行下一次while循环 if res == 'q': return 'q' elif not choice or choice not in dic: continue def threeTL(dic): while True: for key in dic: print(key) # 第二次while循环打印 <陕西省、四川省>(第一次while循环是在递归函数深入的时候执行) choice = input('>>>').strip() # 第二次while循环到此,用户输入 b if choice == 'b' or choice == 'q': return choice # 返回'b',整个程序退出,递归结束 elif choice in dic and dic[choice]: res = threeTL(dic[choice]) # 递归返回 threeTL(dic['陕西省']) if res == 'q': return 'q' elif not choice or choice not in dic: continue
(3)当用户输入 'q'
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
def threeTL(dic): # dic = dic['陕西省']['西安'] while True: for key in dic: print(key) # 未央莲湖高新 choice = input('>>>').strip() # # 用户输入:q if choice == 'b' or choice == 'q': return choice # 返回'q',程序退出 elif choice in dic and dic[choice]: res = threeTL(dic[choice]) if res == 'q': return 'q' elif not choice or choice not in dic: continue def threeTL(dic): # dic = dic['陕西省'] while True: for key in dic: print(key) # <西安、咸阳> choice = input('>>>').strip() if choice == 'b' or choice == 'q': return choice elif choice in dic and dic[choice]: res = threeTL(dic[choice]) # res = 'q' if res == 'q': return 'q' # 匹配到 res='q' 返回 'q' 程序退出 elif not choice or choice not in dic: continue def threeTL(dic): # dic = dic['陕西省']['西安'] while True: for key in dic: print(key) # 未央莲湖高新 choice = input('>>>').strip() # # 用户输入:q if choice == 'b' or choice == 'q': return choice # 返回'q',程序退出 elif choice in dic and dic[choice]: res = threeTL(dic[choice]) if res == 'q': return 'q' elif not choice or choice not in dic: continue
本文作者:hukey
本文链接:https://www.cnblogs.com/hukey/p/9336233.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
· 用 DeepSeek 给对象做个网站,她一定感动坏了
· DeepSeek+PageAssist实现本地大模型联网
· 手把手教你更优雅的享受 DeepSeek
· 腾讯元宝接入 DeepSeek R1 模型,支持深度思考 + 联网搜索,好用不卡机!
· 从 14 秒到 1 秒:MySQL DDL 性能优化实战