python中的高阶函数

1、什么是高阶函数?

高阶函数是指将函数作为参数传入。就是高阶函数

2、高阶函数有哪些?

map 映射函数

python 复制代码
>>> print(list(map(lambda x:x*x,range(1,11))))
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
>>> print(list(map(lambda x:str(x),[1,2,3,4,5])))
['1', '2', '3', '4', '5']
>>>

filter 过滤函数

python 复制代码
>>> print(list(filter(lambda x:x%3==0,range(1,101))))
[3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99]
>>> print(list(filter(lambda x:x %2 == 0,range(1,11))))
[2, 4, 6, 8, 10]
>>> print(list(filter(lambda x:x%2,range(1,11))))
[1, 3, 5, 7, 9]
>>> 

reduce 函数累积求值(需要导入from functools import reduce)

python 复制代码
>>> n = [1,2,3,4,5,6,7]
>>> from functools import reduce
>>> print(reduce(lambda n,y:n*y,n))
5040

sorted 排序函数

python 复制代码
>>> list1=[7, -8, 5, 4, 0, -2, -5]
>>> print(sorted(list1,key=lambda x:max(list1)-x+1 if x <=0 else x))
[4, 5, 7, 0, -2, -5, -8]
>>> print(list(sorted(list1,key=lambda x:(x<=0,abs(x)))))
[4, 5, 7, 0, -2, -5, -8]
>>> 
>>> list1 = ['bob','about','Zoo','Credit']
>>> print(sorted(list1,key=lambda x:x.lower()))
['about', 'bob', 'Credit', 'Zoo']
相关推荐
亿元程序员4 分钟前
自从有了 AI,我就再也不想拼 UI 了……
前端
happylifetree4 分钟前
Python06(补充4):PyCharm里的删除
python·pycharm
数据狐(Datafox)4 分钟前
淘宝商品详情API实战:多语言代购商城自动同步数据完整方案
开发语言·前端·数据库·爬虫·json
打工仔折腾 AI9 分钟前
蓝耘元生代实测:用WorkBuddy与TextIn xParse拆解43页建模论文
人工智能·后端·python·数学建模·langchain·ai agent 实战
芯次元玩家13 分钟前
技术岗转物联网解决方案架构师,行业洞察、商业模式该从哪开始学?
java·开发语言
EatFan14 分钟前
Spring Boot 4 升级避坑指南:依赖变化、Undertow 弃用、4.0.5 补丁与 Java 25 虚拟线程落地
java·spring boot·微服务·虚拟线程·spring boot 4
用户03321266636714 分钟前
Python 实现 Excel 转 XML:快速上手
python·excel
hasty15 分钟前
HTML 已经转义,为何仍有 XSS?Sharp srcdoc 漏洞中的第二次解析
前端·html·xss
涛涛ing17 分钟前
WebMCP正式进入Origin Trial:AI Agent不再“装人”,前端交互范式正在被重写
前端
卓怡学长22 分钟前
w213基于Spring Boot框架的Web安全学习系统的设计与实现
java·spring boot·spring·web安全·intellij-idea