itemgetter和attrgetter能替代从序列中取出元素或读取对象属性的lambda表达式,会自动构建函数

itemgetter 

1,根据元组某个字段给元组列表排序,下例中
itemgetter(1) ==   lambda field : field[1]

2,如果把多个参数传给itemgetter ,它构建的函数会返回提取的值构成元组
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它创建的函数根据名称提取对象的属性
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,这样参数更少

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]


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'}