simple cases about functional programming
2015-10-30 15:57
453 查看
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
相关文章推荐
- JQuery UI 精品UI推荐
- springmvc上传多图片
- 【投稿】iOS自定义键盘
- 我的新博客地址:https://www.iliul.com/
- 给echarts力导向布局图增加滚动条。
- getchar 详解
- DS-5教程-ARM DS-5 添加新芯片至DS-5调试设备列表
- POJ2114(树分治)
- fastjson文档解读——上篇
- winform,wpf全屏 还显示任务栏的解决方法
- 缄默者的振臂高呼
- Nim Game
- JQuery UI 精品UI推荐
- springmvc 环境配置图
- JavaWeb 学习
- inspect a function object
- UE4 Asynchronous Asset Loading
- More Effective C++学习笔记(2)
- SQL Server 2008 数据库编程摘要一 : 函数篇
- FMDB下的数据库操作DML语言