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']
相关推荐
万粉变现经纪人34 分钟前
如何解决 pip install -r requirements.txt 私有索引未设为 trusted-host 导致拒绝 问题
开发语言·python·scrapy·flask·beautifulsoup·pandas·pip
毕业设计制作和分享40 分钟前
springboot150基于springboot的贸易行业crm系统
java·vue.js·spring boot·后端·毕业设计·mybatis
查士丁尼·绵2 小时前
笔试-九宫格三阶积幻方
python·九宫格·三阶积幻方
键盘不能没有CV键3 小时前
【图片处理】✈️HTML转图片字体异常处理
前端·javascript·html
yantuguiguziPGJ4 小时前
WPF 联合 Web 开发调试流程梳理(基于 Microsoft.Web.WebView2)
前端·microsoft·wpf
l1t4 小时前
DeepSeek辅助利用搬移底层xml实现快速编辑xlsx文件的python程序
xml·开发语言·python·xlsx
大飞记Python4 小时前
部门管理|“编辑部门”功能实现(Django5零基础Web平台)
前端·数据库·python·django
tsumikistep5 小时前
【前端】前端运行环境的结构
前端
你的人类朋友5 小时前
【Node】认识multer库
前端·javascript·后端
Aitter5 小时前
PDF和Word文件转换为Markdown的技术实现
前端·ai编程