人理解循环,神理解递归 I am the god hahaha
递归函数
初识递归。
def func():
print('in func')
def func1():
func()
func1()
比如:
def func():
print(666)
func()
func()
def func(n):
print(n)
n += 1
func(n)
func(1)
官网规定:默认递归的最大深度1000次。
如果你递归超过100次还没有解决这个问题,那么执意使用递归,效率很低。
import sys
print(sys.setrecursionlimit(1000000))
def func(n):
print(n)
n += 1
func(n)
func(1)
#
'''
1 太白 18
2 景女神 18 + 2
3 宝元 18 + 2 + 2
4 alex 18 + 2+ 2+2 age(4) = age(3) + 2
'''
def age(n):
if n == 1:
return 18
else:
return age(n-1) + 2
# print(age(4))
def age(4):
if n == 1:
return 18
else:
return age(3) + 2
age(4) = age(3) + 2
def age(3):
if n == 1:
return 18
else:
return age(2) + 2
age(4) = age(2) + 2 + 2
def age(2):
if n == 1:
return 18
else:
return age(1) + 2
age(4) = age(1) + 2 + 2 + 2
def age(1):
if n == 1:
return 18
else:
return age(1) + 2
age(4) = 18 + 2 + 2 + 2
l1 = [1, 3, 5, ['太白','元宝', 34, [33, 55, [11,33]]], [77, 88],66]
'''
1
3
5
'太白'
'元宝'
34
66
'''
l1 = [1,3,5,['太白','元宝',34],55]
l2 = [1, 3, 5, ['太白','元宝', 34, [33, 55, [11,33]]], [77, 88],66]
for i in l1:
if type(i) == list:
for j in i:
print(j)
else:
print(i)
def func(alist):
for i in alist:
if type(i) == list:
func(i) # func(['太白','元宝',34])
else:
print(i)
func(l1)
func(l2)