一、递归的定义
1、定义
# 函数的递归调用:是函数嵌套调用的一种特殊形式 # 具体是指:在调用一个函数的过程中又直接或者间接地调用到本身
2、直接调用与间接调用
#直接调用 # 直接调用本身 # def f1(): # print('是我是我还是我') # f1() # f1() #间接调用 # 间接接调用本身 # def f1(): # print('===>f1') # f2() # # def f2(): # print('===>f2') # f1() # # f1()
递归的本质就是循环。
需要强调的的一点是: 递归调用不应该无限地调用下去,必须在满足某种条件下结束递归调用
二、递归的两个阶段
1、回溯与逆推
# 回溯:一层一层调用下去 # 递推:满足某种结束条件,结束递归调用,然后一层一层返回 # age(5) = age(4) + 10 # age(4) = age(3) + 10 # age(3) = age(2) + 10 # age(2) = age(1) + 10 # age(1) = 18
# def age(n): # if n == 1: # return 18 # return age(n-1) + 10 # # # res=age(5) # print(res)
三、递归的应用
l=[1,2,[3,[4,[5,[6,[7,[8,[9,10,11,[12,[13,]]]]]]]]]]
l = [1, 2, [3, [4, 5, 6, [7, 8, [9, 10, [11, 12, 13, [14, 15]]]]]]] value_list = [] def get_value(lst): for item in lst: if type(item) is list: get_value(item) else: value_list.append(item) get_value(l) print(value_list)