生成器的三个方法
gen = (i for i in range(0, 20, 2))
print(next(gen)) # 0
print(next(gen)) # 2
gen.close()
print(next(gen))
"""
Traceback (most recent call last):
File "/Users/zhongxin/Desktop/py/zx/03/0809.py", line 5, in <module>
print(next(gen))
StopIteration
"""
gen = (i for i in range(0, 20, 2))
gen.throw(NameError, 'name is xxx')
"""
Traceback (most recent call last):
File "/Users/zhongxin/Desktop/py/zx/03/0809.py", line 2, in <module>
gen.throw(NameError, 'name is xxx')
File "/Users/zhongxin/Desktop/py/zx/03/0809.py", line 1, in <genexpr>
gen = (i for i in range(0, 20, 2))
NameError: name is xxx
"""
在函数中调用函数自身,我们把这种函数叫做递归函数 递归边界:递归的终止条件
def func(n):
print(n)
func(n - 1)
func(10)
"""
Traceback (most recent call last):
File "/Users/zhongxin/Desktop/py/zx/03/0809_1.py", line 5, in <module>
func(10)
File "/Users/zhongxin/Desktop/py/zx/03/0809_1.py", line 3, in func
func(n - 1)
File "/Users/zhongxin/Desktop/py/zx/03/0809_1.py", line 3, in func
func(n - 1)
File "/Users/zhongxin/Desktop/py/zx/03/0809_1.py", line 3, in func
func(n - 1)
[Previous line repeated 993 more times]
File "/Users/zhongxin/Desktop/py/zx/03/0809_1.py", line 2, in func
print(n)
RecursionError: maximum recursion depth exceeded while calling a Python object
"""
def func1(n):
print(n)
if n == 1:
return
else:
func(n - 1)
func1(10)
"""
10
9
8
7
6
5
4
3
2
1
"""
实现一个累加函数
1 1
2 1+2
3 1+2+3
4 1+2+3+4
5 1+2+3+4+5
...
def fun_add(n):
if n == 1:
return 1
return n + fun_add(n - 1)
print(fun_add(6))
import sys
print(sys.getrecursionlimit()) # 默认1000 实际996
sys.setrecursionlimit(3000) # 实际2996
调用同样的参数后,使用缓存,不会重新开辟空间
https://docs.python.org/zh-cn/3/library/functools.html
@functools.lru_cache(maxsize=128, typed=False)
一个为函数提供缓存功能的装饰器,缓存 maxsize 组传入参数,在下次以相同参数调用时直接返回上一次的结果。用以节约高开销或I/O函数的调用时间。 由于使用了字典存储缓存,所以该函数的固定参数和关键字参数必须是可哈希的。
一个题目:
爬100个台阶,爬台阶每一步可以上一阶,两阶,或三阶,请问一共有多少种爬法
from functools import lru_cache
@lru_cache(maxsize=128)
def func999(n):
if n == 1:
return 1
elif n == 2:
return 2
elif n == 3:
return 4
else:
return func999(n - 1) + func999(n - 2) + func999(n - 3)
print(func999(100)) # 180396380815100901214157639
一个函数的返回结果只依赖于它的参数,并且在执行过程里面没有副作用
副作用(side effects)
,不会改变被传入的数据或其他数据(全局变量)函数的副作用:
def func(a, b):
return a * b + 100
print(func(11, 222))
li = [1, 2, 3, 4, 5, 6, 7] # 每个元素*1000/2
def fun(n):
return n * 1000 / 2
res = map(fun, li)
print(res) # <map object at 0x1033050f0>
print(list(res)) # [500.0, 1000.0, 1500.0, 2000.0, 2500.0, 3000.0, 3500.0]
def func1(n):
return n >= 5
res1 = filter(func1,li)
print(res1) # <filter object at 0x103b9f390>
print(list(res1)) # [5, 6, 7]
长度不一样,以最短为准
title = ['name', 'age', 'gender']
data = ['zx', 26, '男']
print(list(zip(title,data))) # [('name', 'zx'), ('age', 26), ('gender', '男')]
print(dict(zip(title,data))) # {'name': 'zx', 'age': 26, 'gender': '男'}
num = [1, 2, 3]
print(list(zip(title, data, num))) # [('name', 'zx', 1), ('age', 26, 2), ('gender', '男', 3)]
关键字:lambda
lambda 参数:表达式(返回值)
res = (lambda n: n * 2)(4)
print(res) # 8
# 偏函数之前的写法
li = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(list(filter(lambda x:x>5,li))) # [6, 7, 8, 9, 10]
from functools import partial
li = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
myfilter = partial(filter, lambda x: x > 5)
print(list(myfilter(li))) # [6, 7, 8, 9, 10]
一个简单的例子:
from functools import partial
def func1(a, b, c):
print(a, b, c)
func1(11, 22, 33)
func1(11, 'bb', 'cc')
func1(11, 'dd', 'ee')
myfilter1 = partial(func1, 11)
myfilter1(22, 33)
myfilter1('bb', 'cc')
myfilter1('dd', 'ee')
myfilter2 = partial(func1, b=22)
myfilter2(a=11, c='cc') # 11 22 cc
myfilter2 = partial(func1, a=11, b=22)
myfilter2(c='cc') # 11 22 cc