filter函数
filter(fn,lsd) 作用:过滤 流程:lsd中所有的元素作用到fn上,如果返回True那么久保留该值,如果返回False那就过滤掉该值
L = [1,2,3,4,5,6,7,8]
def func(key):
if key%2 == 0:
return True
else:
return False
res = list(filter(func,L))
print(res)
F:\学习代码\Python代码\venv\Scripts\python.exe F:/学习代码/Python代码/day6/高阶函数---filter.py
[2, 4, 6, 8]
Process finished with exit code 0