simple cases about functional programming

import functools


def fac(n):
    return functools.reduce(lambda a,b:a*b, range(1,n+1))


fac(5)




120

another solution is using operator module

import operator
def fact(n):
    return functools.reduce(operator.mul, range(1,n+1))


fact(5)




120




metro_data = [
    ('Tokyo', 'JP', 36.933, (35.689722, 139.691667)),
    ('Delhi NCR', 'IN', 21.935, (28.613889, 77.208889)),
    ('Mexico City', 'MX', 20.142, (19.433333, -99.133333)),
    ('New York-Newark', 'US', 20.104, (40.808611, -74.020386)),
    ('Sao Paulo', 'BR', 19.649, (-23.547778, -46.635833))
    ]


for city in sorted(metro_data, key=operator.itemgetter(1)):   # 根据国籍排序 使用itemgetter函数
    print(city)

('Sao Paulo', 'BR', 19.649, (-23.547778, -46.635833))
('Delhi NCR', 'IN', 21.935, (28.613889, 77.208889))
('Tokyo', 'JP', 36.933, (35.689722, 139.691667))
('Mexico City', 'MX', 20.142, (19.433333, -99.133333))
('New York-Newark', 'US', 20.104, (40.808611, -74.020386))



country_city_name = operator.itemgetter(1,0)  # an function get values from index 1 and 0 , then collect them in tuple
for ele in map(country_city_name, metro_data):
    print(ele)

('JP', 'Tokyo')
('IN', 'Delhi NCR')
('MX', 'Mexico City')
('US', 'New York-Newark')
('BR', 'Sao Paulo')

由于itemgetter利用了[]操作符,所以它支持任何实现了__getitem__方法的类型,不局限于序列还包括字典。
类似的还有attrgetter,它也是个函数,以属性名称(字符串类型)作为参数,创建函数,最终作用于实例对象,返回tuple类型。
任何能被.(dot)访问的属性都能被attrgetter访问。

import collections
LatLong = collections.namedtuple('LatLong', 'lat long')
Metropolis = collections.namedtuple('Metropolis', 'name cc pop coord')


metro_areas = [Metropolis(name, cc, pop, LatLong(lat, long)) for name, cc, pop, (lat, long) in metro_data]


metro_areas[0]




Metropolis(name='Tokyo', cc='JP', pop=36.933, coord=LatLong(lat=35.689722, long=139.691667))




metro_areas[0].coord.lat




35.689722




name_lat = operator.attrgetter('name','coord.lat')   # a function get attribute named 'name', 'coord.lat'
for city in sorted(metro_areas, key=operator.attrgetter('coord.lat')): # sort use the attribute 'coord.lat'
    print(name_lat(city))

('Sao Paulo', -23.547778)
('Mexico City', 19.433333)
('Delhi NCR', 28.613889)
('Tokyo', 35.689722)
('New York-Newark', 40.808611)



[name for name in dir(operator) if not name.startswith('_')]




['abs',
 'add',
 'and_',
 'attrgetter',
 'concat',
 'contains',
 'countOf',
 'delitem',
 'eq',
 'floordiv',
 'ge',
 'getitem',
 'gt',
 'iadd',
 'iand',
 'iconcat',
 'ifloordiv',
 'ilshift',
 'imod',
 'imul',
 'index',
 'indexOf',
 'inv',
 'invert',
 'ior',
 'ipow',
 'irshift',
 'is_',
 'is_not',
 'isub',
 'itemgetter',
 'itruediv',
 'ixor',
 'le',
 'length_hint',
 'lshift',
 'lt',
 'methodcaller',
 'mod',
 'mul',
 'ne',
 'neg',
 'not_',
 'or_',
 'pos',
 'pow',
 'rshift',
 'setitem',
 'sub',
 'truediv',
 'truth',
 'xor']




s = 'The time has come'


upcase = operator.methodcaller('upper') # a function
print(upcase(s))

THE TIME HAS COME



replce_maker = operator.methodcaller('replace',' ','-')
print(replce_maker(s))

The-time-has-come



 triple = functools.partial(operator.mul, 3)


triple(7)




21




list(map(triple,range(1,11)))




[3, 6, 9, 12, 15, 18, 21, 24, 27, 30]




import unicodedata


nfc = functools.partial(unicodedata.normalize,'NFC')


s1 = 'café'


s2 = 'cafe\u0301'


s1




'café'




s2




'café'




s2 == s1




False




nfc(s1)




'café'




nfc(s2)




'café'




nfc(s1) == nfc(s2)




True
全部评论

相关推荐

不愿透露姓名的神秘牛友
昨天 12:31
以前小时候我最痛恨出轨、偷情的人,无论男女,为什么会出轨?现在我成了自己最讨厌的人,没想到分享的东西在牛客会被这么多人看,大家的评价都很中肯,我也认同,想过一一回复,但我还是收声了,我想我应该说说这件事,这件事一直压在我心里,是个很大的心结,上面说了人为什么出轨,我大概能明白了。我们大一下半年开始恋爱,开始恋爱,我给出了我铭记3年的承诺,我对她好一辈子,我永远不会背叛,我责任心太重,我觉得跟了我,我就要照顾她一辈子,我们在一起3年我都没有碰过她,她说往东我就往东,她说什么我做什么,她要我干什么,我就干什么!在学校很美好,中途也出过一些小插曲,比如男闺蜜、男闺蜜2号等等等。但我都强迫她改掉了,我...
牛客刘北:两个缺爱的人是没有办法好好在一起的,但世界上哪有什么是非对错?你后悔你们在一起了,但是刚刚在一起的美好也是真的呀,因为其他人的出现,你开始想要了最开始的自己,你的确对不起自己,21岁的你望高物远,你完全可以不谈恋爱,去过你想要的生活,你向往自由,在一起之后,你要想的不是一个人,而是两个人,你不是变心了,就像你说的,你受够了,你不想包容了,冷静几天是你最优的选择,爱人先爱己。
社会教会你的第一课
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-10 15:58
投个小米提前批试试水,先投一个岗位看看形势,不行就再沉淀一下投第二个岗位,莫辜负
Java抽象带篮子:我嘞个骚刚,已经开始研发6g了吗
投递小米集团等公司7个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务