python高级函数有几个?
python高级函数有几个?
本文教程操作环境:windows7系统、Python3.9.1,DELLG3电脑。
1、filter
用于筛选数据,只保留满足条件的值filter(function,iterable)
两个参数:第一个是函数,第二个是迭代数据。
#筛选100以内个位数为1的元素。
list(filter(lambdax:x%10==1,range(100)))
(Output:[1,11,21,31,41,51,61,71,81,91])
2、map
对数据所有元素执行同一操作。
map(function,iterable)
3、reduce
对数据的所有元素依次累积进行计算,首先计算前两个元素的函数结果,再计算该结果和第三个元素的函数结果,依次类推。
reduce(function,iterable[,initializer])
4、sorted
对数据排序。
sorted(iterable,key=None,reverse=False)
思考:如何计算5以内的数的平方和?对5以内的正数x计算(x-2)**2,并逆序排序。
reduce(lambdax,y:x+y,map(lambdax:x*x,range(5)))
(Output:30)
sorted(map(lambdax:(x-2)**2,range(1,5)),reverse=True)
(Output:[4,1,1,0])
以上就是python中的4种高级函数,大家学完后会发现其实就是我们常见的基础函数,不过在一些特殊的用法上属于高级的范畴,学会后也赶快展开练习吧。更多Python学习教程请关注IT培训机构:千锋教育。

相关推荐HOT
更多>>
python函数标注是什么
python函数标注是什么在参数的使用方法中,还有一种解包的情况是需要我们掌握的。比如,将列表或者字典的值转换为函数的参数,就需要用到参数解...详情>>
2023-11-14 14:13:35
python正负索引的使用
python正负索引的使用本文教程操作环境:windows7系统、Python3.9.1,DELLG3电脑。1、说明使用正索引(从左至右)、负索引(从右至左)访问元素,时...详情>>
2023-11-14 13:45:04
python垃圾回收的机制过程
python垃圾回收的机制过程本文教程操作环境:windows7系统、Python3.9.1,DELLG3电脑。1、步骤(1)收集所有容器对象(list,dict,tuple,customClas...详情>>
2023-11-14 13:05:15
pythontry语句的执行原理
pythontry语句的执行原理1、原理分析(1)执行try子句(try和except关键字之间的多行语句)。(2)如果没有异常发生,则跳过except子句并完成try语句...详情>>
2023-11-14 09:37:35热门推荐
技术干货






