14、函数的递归调用、生成式、匿名函数
作者:互联网
一、叠加多个装饰器
def deco1(func1): # func1=函数wrapper2的内存地址
def wrapper1(*args,**kwargs):
print('=========>wrapper1')
res1 = func1(*args,**kwargs)
return res1
return wrapper1
def deco2(func2): # func2=函数wrapper3的内存地址
def wrapper2(*args,**kwargs):
print('=========>wrapper2')
res2 = func2(*args,**kwargs)
return res2
return wrapper2
def deco3(func3): # func3 = 最原始那个index函数的内存地址
def wrapper3(*args,**kwargs):
print('=========>wrapper3')
res3 = func3(*args,**kwargs)
return res3
return wrapper3
# index=函数wrapper1的内存地址
@deco1 # deco1(函数wrapper2的内存地址)->函数wrapper1的内存地址
@deco2 # deco2(函数wrapper3的内存地址)->函数wrapper2的内存地址
@deco3 # deco3(最原始那个index函数的内存地址)->函数wrapper3的内存地址
def index():
print('=------------>index')
return 123
res = index()
print(res)
# print(index)
二、函数的递归调用
"""
1 什么是函数递归调用
函数递归调用是函数嵌套调用的一种特殊格式,具体是指在调用一个函数的过程
又直接或者间接地调用自己
函数递归调用不应该无限递归调用下去,应该在满足某种条件下结束递归调用
所以,函数递归应该分为两个阶段:
1、回溯
2、递推
2 为何要用
重复运行代码的第三种方案
3 如何用
"""
def f1():
print('hello1')
print('hello2')
print('hello3')
f1()
f1()
while True:
print('hello1')
print('hello2')
print('hello3')
def f1():
print('f1=====>')
f2()
def f2():
print('f2====>')
f1()
f1()
age(5) = age(4) + 10
age(4) = age(3) + 10
age(3) = age(2) + 10
age(2) = age(1) + 10
age(1) = 30
n > 1 age(n) = age(n-1) + 10
n = 1 age(1) = 30
def age(n):
if n == 1:
return 30
return age(n-1) + 10
res = age(5)
print(res)
import sys
print(sys.getrecursionlimit())
sys.setrecursionlimit(2000)
l = [1,[2,[3,[4,[5,[6,[7,[8,[9]]]]]]]]]
def foo(l):
for item in l:
if type(item) is not list:
print(item)
else:
foo(item)
foo(l)
基于递归实现二分法
nums = [-3, 5, 7, 11, 21, 31, 41, 53, 67, 77, 83, 99, 101]
def search(nums,find_num):
print(nums)
if len(nums) == 0:
print('不存在')
return
mid_index = len(nums) // 2
if find_num > nums[mid_index]:
# 在右半部分
search(nums[mid_index+1:],find_num)
elif find_num < nums[mid_index]:
# 在左半部分
search(nums[:mid_index],find_num)
else:
print('找到了')
# search(nums,67)
search(nums,69)
三、生成式
一 列表生成式
l = []
for i in range(1, 6):
if i > 3:
l.append(i)
print(l)
l = [i for i in range(1,6)]
l = [i**2 for i in range(1,6)]
l = [i for i in range(1,6) if i > 3]
print(l)
names = ['liusir_dsb', 'housir_dsb', 'egon', 'wusir_dsb']
res = [name for name in names if name.endswith('dsb')]
print(res)
二 字典生成式
res = {k:v for k,v in [('name', 'egon'), ('age', 18)]}
res = {i:i for i in range(5)}
print(res)
三 集合生成式
res = {i for i in range(5)}
print(res,type(res))
四 生成器表达式
l = [i for i in range(1,6)]
g = (i for i in range(1,6))
print(g)
print(next(g))
print(next(g))
print(next(g))
print(next(g))
print(next(g))
print(next(g))
with open('a.txt',mode='rt',encoding='utf-8') as f:
res = f.read()
print(len(res))
res = 0
for line in f:
res += len(line)
#res = sum([len(line) for line in f])
#res = sum((len(line) for line in f))
res = sum(len(line) for line in f)
print(res)
四、匿名函数
# 匿名函数只能用一次
def foo(x, y):
return x + y
# 调用匿名函数方式一
f=lambda x,y:x+y
print(f(1,2))
# 调用匿名函数方式二
res = (lambda x,y:x+y)(1,2)
print(res)
# 匿名函数真正的用途是与其他函数配合使用
salaries = {
'axx': 30000,
'bxx': 3000,
'cxx': 2000,
'egon': 1000,
'zxx': 500
}
print(max([33,44,11,99]))
def func(k):
return salaries[k]
print(max(salaries,key=func))
print(max(salaries,key=lambda k:salaries[k]))
print(min(salaries,key=lambda k:salaries[k]))
print(sorted(salaries,key=lambda k:salaries[k]))
print(sorted(salaries,key=lambda k:salaries[k],reverse=True))
标签:index,return,函数,res,生成式,print,def,14 来源: https://www.cnblogs.com/daishenmin/p/15399654.html