python之路_递归函数及实例讲解
一、递归
一个函数在内部调用自己的函数称为递归,递归的次数在python是有限制的,默认递归次数是997次。
count=0 def func(): global count count+=1 print(count) func() func() #最大打印出997,最后报错:RecursionError
修改默认递归次数的方法如下:
import sys sys.setrecursionlimit(修改后的值)
二、递归实例
写递归函数必须要有结束条件
1、alex年龄实例:alex比*大2岁,*比**大2岁,**比***大2岁,***40岁
def age(n): if n==4: return 40 return age(n+1)+2 print(age(1)) #age(1)>>>age(2)+2>>>age(3)+2+2>>>age(4)+2+2+2>>>46
2、数字n阶乘实例
def func(n): if n==1: return 1 else: return n*func(n-1) print(func(7))
3、二分法查找列表l中数字num的索引
#查找的数必须存在的情况 def search(l,num,start=None,end=None): start=start if start else 0 end=end if end else len(l)-1 mid=(end-start)//2+start if l[mid]>num: return search(l,num,start,mid-1) elif l[mid]<num: return search(l,num,mid+1,end) elif l[mid]==num: return mid l = [2, 3, 5, 10, 15, 16, 18, 22, 26, 30, 32, 35, 41, 42, 43, 55, 56, 66, 67, 69, 72, 76, 82, 83, 88] print(search(l, 68))
#兼容查找的数不存在的情况 def search(l,num,start=None,end=None): start=start if start else 0 end=end if end else len(l)-1 mid=(end-start)//2+start if start>end: return None elif l[mid]>num: return search(l,num,start,mid-1) elif l[mid]<num: return search(l,num,mid+1,end) elif l[mid]==num: return mid l = [2, 3, 5, 10, 15, 16, 18, 22, 26, 30, 32, 35, 41, 42, 43, 55, 56, 66, 67, 69, 72, 76, 82, 83, 88] print(search(l, 68))
4、三级菜单实例:
menu = { '北京': { '海淀': { '五道口': { 'soho': {}, '网易': {}, 'google': {} }, '中关村': { '爱奇艺': {}, '汽车之家': {}, 'youku': {}, }, '上地': { '百度': {}, }, }, '昌平': { '沙河': { '老男孩': {}, '北航': {}, }, '天通苑': {}, '回龙观': {}, }, '朝阳': {}, '东城': {}, }, '上海': { '闵行': { "人民广场": { '炸鸡店': {} } }, '闸北': { '火车战': { '携程': {} } }, '浦东': {}, }, '山东': {}, } def three_level_menu(menu): while True: for key in menu: print(key) k=input('>>>') if k=='q':return 'q' elif k=='b':break elif k in menu: ret=three_level_menu(menu[k]) if ret=='q':return 'q' three_level_menu(menu)
5、输出斐波那契第n个数
def fib(n): if n==1 or n==2: return 1 return fib(n-1)+fib(n-2)
6、有一个数据结构如下所示,请编写⼀个函数从该结构数据中返回由指定的字段和对应的值组成的字典。如果指定字段不存在,则跳过该字段。
data={"time":"2016-08-05T13:13:05",
"some_id":"ID1234",
"grp1":{ "fld1":1,"fld2":2},
"xxx2":{ "fld3":0,"fld5":0.4},
"fld6":11,
"fld7":7,
"fld46":8}
fields:由"|"连接的以"fld"开头的字符串,如:fld2|fld3|fld7|fld19
#思路一: def select(data,fields): result = {} field_lst = fields.split('|') for key in data: if key in field_lst: result[key] = data[key] elif type(data[key]) == dict: res = select(data[key],fields) result.update(res) return result data={"time":"2016-08-05T13:13:05", "some_id":"ID1234", "grp1":{ "fld1":1,"fld2":2}, "xxx2":{ "fld3":0,"fld5":0.4}, "fld6":11, "fld7":7, "fld46":8} fields = 'fld2|fld3|fld7|fld19' print(select(data,fields))
#思路二: def select(data,fields,result = {}): field_lst = fields.split('|') for key in data: if key in field_lst: result[key] = data[key] elif type(data[key]) == dict: select(data[key], fields) return result data={"time":"2016-08-05T13:13:05", "some_id":"ID1234", "grp1":{ "fld1":1,"fld2":2}, "xxx2":{ "fld3":0,"fld5":0.4}, "fld6":11, "fld7":7, "fld46":8} fields = 'fld2|fld3|fld7|fld19' select(data,fields) print(select(data,fields))