itemgetter和attrgetter能替代从序列中取出元素或读取对象属性的lambda表达式,会自动构建函数
itemgetter
1,根据元组某个字段给元组列表排序,下例中
1 | itemgetter(1) == lambda field : field[1]2,如果把多个参数传给itemgetter ,它构建的函数会返回提取的值构成元组 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | metro_data = [ ( "tykyo" , 'jp' ,36.944,(35.68944,139.69166)), ( "delhi ncr" , 'in' ,21.935,(36.64944,138.79166)), ( "mexico city" , 'mx' ,46.944,(33.68944,129.69166)), ( "new york-newark" , 'us' ,20.944,(35.68944,139.69166)), ( "sao paulo" , 'br' ,16.944,(25.68944,149.69166)) ] #1 from operator import itemgetter for city in sorted(metro_data,key=itemgetter(1)): print(city) #2 cc_name=itemgetter(1,0) for city in metro_data: print(cc_name(city)) |
返回:
('sao paulo', 'br', 16.944, (25.68944, 149.69166))
('delhi ncr', 'in', 21.935, (36.64944, 138.79166))
('tykyo', 'jp', 36.944, (35.68944, 139.69166))
('mexico city', 'mx', 46.944, (33.68944, 129.69166))
('new york-newark', 'us', 20.944, (35.68944, 139.69166))-------------------------------------------------------------------------------------
('jp', 'tykyo')
('in', 'delhi ncr')
('mx', 'mexico city')
('us', 'new york-newark')
('br', 'sao paulo')
attrgetter
它创建的函数根据名称提取对象的属性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | metro_data = [ ( "tykyo" , 'jp' ,36.944,(35.68944,139.69166)), ( "delhi ncr" , 'in' ,21.935,(36.64944,138.79166)), ( "mexico city" , 'mx' ,46.944,(33.68944,129.69166)), ( "new york-newark" , 'us' ,20.944,(35.68944,139.69166)), ( "sao paulo" , 'br' ,16.944,(25.68944,149.69166)) ] from operator import itemgetter from collections import namedtuple LatLong = namedtuple( 'LatLong' , 'lat long' )#定义LatLong Metropolis = namedtuple( 'Metropolis' , 'name cc pop coord' )#定义Metropolis #使用嵌套的元祖拆包提取(Lat,Long) Metro_areas = [Metropolis(name,cc,pop,LatLong(Lat,Long)) for name,cc,pop,(Lat,Long) in metro_data] print(Metro_areas) print(Metro_areas[0]) print(Metro_areas[0].coord) print(Metro_areas[0].coord.lat) from operator import attrgetter name_lat = attrgetter( 'name' , 'coord.lat' ) for city in sorted(Metro_areas,key=attrgetter( 'coord.lat' )): print(name_lat(city)) |
返回:
[Metropolis(name='tykyo', cc='jp', pop=36.944, coord=LatLong(lat=35.68944, long=139.69166)), Metropolis(name='delhi ncr', cc='in', pop=21.935, coord=LatLong(lat=36.64944, long=138.79166)), Metropolis(name='mexico city', cc='mx', pop=46.944, coord=LatLong(lat=33.68944, long=129.69166)), Metropolis(name='new york-newark', cc='us', pop=20.944, coord=LatLong(lat=35.68944, long=139.69166)), Metropolis(name='sao paulo', cc='br', pop=16.944, coord=LatLong(lat=25.68944, long=149.69166))]
Metropolis(name='tykyo', cc='jp', pop=36.944, coord=LatLong(lat=35.68944, long=139.69166))
LatLong(lat=35.68944, long=139.69166)
35.68944
('sao paulo', 25.68944)
('mexico city', 33.68944)
('tykyo', 35.68944)
('new york-newark', 35.68944)
('delhi ncr', 36.64944)
functools.partial
:冻结参数
:基于一个函数创建一个新的可调用对象,把原函数的某些参数固定,使用这个函数可以吧接受一个或多个参数的函数改编成需要回调的API,这样参数更少
1 2 3 4 5 6 7 8 9 | from operator import mul from functools import partial triple = partial(mul,3) print(triple(7)) l = list(map(triple,range(1,10))) print(l) 返回: 21 [3, 6, 9, 12, 15, 18, 21, 24, 27] |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | def tag(name,*content,cls=None,**attrs): if cls is not None: attrs[ 'class' ] = cls if attrs: attrs_str = '' . join ( ' %s="%s" ' % (attr,value) for attr,value in sorted(attrs.items())) else : attrs_str= '' if content: return '\n' . join ( '<%s %s >%s</%s>' % (name,attrs_str,c,name) for c in content) else : return '<%s%s />' % (name,attrs_str) from functools import partial picture = partial(tag, 'img' ,cls= 'pic-frame' ) print(picture) p = picture(src= 'sunlong.jpeg' ) print(p) print(picture.args) print(picture.keywords) 返回: functools.partial(<function tag at 0x00000000022178C8>, 'img' , cls= 'pic-frame' ) <img class = "pic-frame" src= "sunlong.jpeg" /> ( 'img' ,) { 'cls' : 'pic-frame' } |
本文来自博客园,作者:孙龙-程序员,转载请注明原文链接:https://www.cnblogs.com/sunlong88/articles/10426361.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· DeepSeek在M芯片Mac上本地化部署
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能