python中匿名函数关键字(浅谈python之高阶函数和匿名函数)
python中匿名函数关键字
浅谈python之高阶函数和匿名函数map()
map()函数接收两个参数,一个是函数,一个是Iterable,map将传入的函数依次作用到序列的每个元素,并把结果作为新的Iterator返回。
|
def func(x): return x * x r = map (func, [ 1 , 2 , 3 , 4 , 5 ]) print ( type (r)) r = list (r) print (r) |
输出结果:
<class 'map'>
[1, 4, 9, 16, 25]
可以看出,map让函数func作用于列表的每一项,使列表的每一项都被函数func执行一次,即列表的每一项都进行平方。其返回值是map类型。
reduce()
reduce函数必须接收两个参数,把一个函数作用在一个序列[x1, x2, x3, ...]上,然后再把结果继续和序列的下一个元素做累积计算。
|
from functools import reduce def fn(x, y): return x * 10 + y f = reduce (fn, [ 1 , 3 , 5 , 7 , 9 ]) # 把序列变为整数 print (f) print ( type (f)) |
输出结果:
13579
<class 'int'>
和map不同,虽然reduce也是作用于每个元素,但是reduce的作用结果要用在下次和另一个元素做累积计算。
map()和reduce()的结合使用
|
from functools import reduce def fn(x, y): return x * 10 + y def char2num(s): digits = { '0' : 0 , '1' : 1 , '2' : 2 , '3' : 3 , '4' : 4 , '5' : 5 , '6' : 6 , '7' : 7 , '8' : 8 , '9' : 9 } return digits[s] f = reduce (fn, map (char2num, '13579' )) print (f) |
输出结果:
13579
可以将字符串类型转换为int类型
filter()
filter()函数用于过滤序列,接收一个函数和一个序列。和map()不同的是,filter()把传入的函数依次作用于每个元素,然后根据返回值是True还是False决定保留还是丢弃该元素。
|
def not_empty(s): return s and s.strip() # 不能直接写s.strip() f = filter (not_empty, [ 'A' , ' ', ' B ', None, ' C ', ' ']) print ( type (f)) print ( list (f)) |
输出结果:
<class 'filter'>
['A', 'B', 'C']
sorted()
sorted()函数也是一个高阶函数,在列表的学习中初步接触了sorted(),但其实它还可以接收一个key函数来实现自定义的排序。
key指定的函数将作用于被排序对象的每一个元素上,并根据key函数返回的结果进行排序。
|
l = sorted ([ 36 , 5 , - 12 , 9 , - 21 ], key = abs ) # 按绝对值大小排序 print (l) s = sorted ([ 'bob' , 'about' , 'Zoo' , 'Credit' ]) # 按ASCII大小排序 print (s) sl = sorted ([ 'bob' , 'about' , 'Zoo' , 'Credit' ], key = str .lower) # 忽略大小写排序 print (sl) sr = sorted ([ 'bob' , 'about' , 'Zoo' , 'Credit' ], key = str .lower, reverse = True ) # 反向排序 print (sr) |
输出结果:
[5, 9, -12, -21, 36]
['Credit', 'Zoo', 'about', 'bob']
['about', 'bob', 'Credit', 'Zoo']
['Zoo', 'Credit', 'bob', 'about']
同样的,sorted()也可以对元组和字典进行排序
|
from operator import itemgetter # 需要使用operator模块 L = [( 'Bob' , 75 ), ( 'Adam' , 92 ), ( 'Bart' , 66 ), ( 'Lisa' , 88 )] print ( sorted (L, key = itemgetter( 0 ))) |
输出结果:
[('Adam', 92), ('Bart', 66), ('Bob', 75), ('Lisa', 88)]
当然,也能以values的值来排序,对字典的排序与元组类似,但返回值不同。
|
from operator import itemgetter dic = { "Bob" : 75 , "Adam" : 92 , "Lisa" : 88 } print ( sorted (dic,key = itemgetter( 0 ))) print ( sorted (dic, key = itemgetter( 1 ))) |
输出结果:
['Adam', 'Bob', 'Lisa']
['Adam', 'Lisa', 'Bob']
可以看到,不管是以key值进行排序,还是以value值进行排序,排序结果只返回对应顺序的key值。
匿名函数
|
f = list ( map ( lambda x: x * x, [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ])) print (f) |
输出结果:
[1, 4, 9, 16, 25, 36, 49, 64, 81]
上面就是一个匿名函数的使用,匿名函数 lambda x: x * x 实际上就是:
|
def f(x): return x * x |
只是在这里没有显式地定义函数,这样因为函数没有名字,不必担心函数名冲突,而且代码看起来也简洁。
以上所述是小编给大家介绍的python之高阶函数和匿名函数详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对开心学习网网站的支持!
原文链接:https://www.cnblogs.com/zuoxide/p/10557870.html
- 如何使用python定时运行(Python实现定时执行任务的三种方式简单示例)
- python基础编程函数参数(详解Python 函数如何重载?)
- pythongui实战案例(Python GUI编程完整示例)
- python中读取文件怎么操作(Python实现的读取文件内容并写入其他文件操作示例)
- python3.7手册中文版(Python3.4解释器用法简单示例)
- python中的多线程详解(python多线程抽象编程模型详解)
- python的decode函数在哪个模块(python中报错"json.decoder.JSONDecodeError: Expecting value:"的解决)
- python中tkinter模块窗口操作(详解python tkinter教程-事件绑定)
- pythonzipfile的用法(对Python之gzip文件读写的方法详解)
- flask项目微信小程序(Python Flask 搭建微信小程序后台详解)
- python获取网络数据tcp(Python选择网卡发包及接收数据包)
- python中return 类的实例(Python实现Event回调机制的方法)
- python3和python区别(Python2与Python3的区别实例总结)
- python中的insert(python assert的用处示例详解)
- python实现购物网站(Python实战购物车项目的实现参考)
- python简单代码实例(Python实现 版本号对比功能的实例代码)
- 乔欣古装女主戏获热度 作为女主,却没吃到红利(乔欣古装女主戏获热度)
- 爱情是什么(爱情是什么最经典的话)
- 乔欣 古装剧中的高颜值(古装剧中的高颜值)
- 怎么才可以财富自由(如何让自己实现财富自由)
- 为什么越来越多年轻人回农村(为什么越来越多年轻人回农村生活)
- 怎么快速学好英语(怎么快速学好英语初中)
热门推荐
- mysql用户删除了如何设置(MySQL两种删除用户语句的区别delete user和drop user)
- dedecms栏目静态化(DedeCms获取任意栏目N级列表链接树形菜单的方法)
- phpstudy中php用什么版本(phpstudy是什么?phpstudy有什么用?)
- django中的urlpatterns(Django web框架使用url path name详解)
- tomcat在linux系统安装步骤(Tomcat的卸载和重装的实现图文)
- 微信推送功能大全(微信推送功能实现方式图文详解)
- MySQL执行事务的语法与流程详解(MySQL执行事务的语法与流程详解)
- 浏览器内核有哪些
- mysqlsql按日期统计(sqlserver/mysql按天、按小时、按分钟统计连续时间段数据推荐)
- 盘点CSS Selectors Level4中新增的选择器(盘点CSS Selectors Level4中新增的选择器)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9