python学习笔记(九)函数式进阶:使用map、reduce、filter、sorted
2017-04-27 21:25
961 查看
在第三节中简单学习了函数式的概念,这里通过运用几个常用的高阶函数来熟悉函数式编程方法,在文末给出一个综合性例子。
在这个过程中产生了副作用:打印元素值,在纯函数式编程中是没有这类副作用的。
列表结构类似于一个链表,每一个结点包含当前元素值以及之后元素的指针,且每个结点有一个“列表头”,因此对其进行reduce操作相当于一个递归函数。
传入的函数必须满足其参数性质为:第一个参数为之后的结点计算得出的元素值,第二个参数为当前的结点(可能为空)。
格式:reduce(函数,列表)
reduce(f,[1,2,3,4]) => f(f(f(1,2),3),4)
具体实现见例子。
其中f1、f2是用闭包得到的函数,可以根据传入的参数选择类的成员作为其排序依据进行排序;eval()是可以将字符串作为python代码执行的函数;使用map是一种简便的遍历列表的写法。
参考资料:
http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/00141861202544241651579c69d4399a9aa135afef28c44000
开始
这四个高阶函数具有相似的参数结构,都是以其中一个参数为列表,另一个参数为函数。不同的是它们的功能。map
map的作用简单来说是将列表中各个元素冠以函数头。即将函数映射作用到每一个元素上.(格式:map(函数,列表))def mp(a): print a return a l = [1,2,3,4] map(mp,l)
在这个过程中产生了副作用:打印元素值,在纯函数式编程中是没有这类副作用的。
reduce
如果说map是把每个元素戴上函数的帽子,那么reduce就是将列表的头直接换成函数头。列表结构类似于一个链表,每一个结点包含当前元素值以及之后元素的指针,且每个结点有一个“列表头”,因此对其进行reduce操作相当于一个递归函数。
传入的函数必须满足其参数性质为:第一个参数为之后的结点计算得出的元素值,第二个参数为当前的结点(可能为空)。
格式:reduce(函数,列表)
reduce(f,[1,2,3,4]) => f(f(f(1,2),3),4)
inorder = [3,2,4,1,6,5,7] def func(other,cur): print other,cur return cur reduce(func,inorder)
filter
filter作用是将满足函数条件的元素全部滤除。因此传入的函数必须为返回true或false的函数。inorder = filter(lambda(a):a>5,inorder)
sorted
sorted作用是按照函数规则进行排序。函数规则是:返回-1代表二者是逆序关系,返回1代表二者是顺序关系,返回0代表二者等序。具体实现见例子。
一个例子
import math import functools class item(object): def __init__(self,x,y): self.__x = x self.__y = y @property def x(self): return self.__x @x.setter def x(self,value): self.__x = value @property def y(self): return self.__y @y.setter def y(self,value): self.__y = value @property def power(self): return math.sqrt(self.__x*self.__x+self.__y*self.__y) def func_sort_with(member): def item_compare(a,b): i1 = eval('a.'+member) i2 = eval('b.'+member) if i1>i2: return -1 elif i2>i1: return 1 return 0 return functools.partial(sorted,cmp = item_compare) def print_name(it): print str(it.x) list_src = [item(i,10-i) for i in range(1,10)] f1 = func_sort_with('x') list_1 = f1(list_src) print 'sort with x:' map(print_name,list_1) f2 = func_sort_with('power') list_2 = f2(list_src) print 'sort with power' map(print_name,list_2)
其中f1、f2是用闭包得到的函数,可以根据传入的参数选择类的成员作为其排序依据进行排序;eval()是可以将字符串作为python代码执行的函数;使用map是一种简便的遍历列表的写法。
参考资料:
http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/00141861202544241651579c69d4399a9aa135afef28c44000
相关文章推荐
- python学习笔记,map/reduce,filter,sorted
- python学习笔记-Day04-第三部分(内置函数,map,filter,reduce,yield)
- Python 函数式编程--高阶函数Map、Reduce、Filter、Sorted
- Python函数式编程学习:lambda, map, reduce, filter
- python笔记【map(),reduce(),filter(),sorted()】
- python学习(9)高阶函数map、reduce、filter、sorted
- [Python学习笔记-002] lambda, map, filter and reduce
- 【Python学习笔记】函数式编程:高阶函数map和reduce
- 【python】详解lambda匿名函数以及结合map、reduce、filter、sorted等使用
- python:内建函数、range()、map()、filter()、reduce()、sorted()、集合的使用、functools中的函数使用、MD5加密
- python--函数式编程 (高阶函数(map , reduce ,filter,sorted),匿名函数(lambda))
- 王亟亟的Python学习之路(八)-函数式编程,map(),reduce(),filter()
- python:内建函数、range()、map()、filter()、reduce()、sorted()、集合的使用、functools中的函数使用、MD5加密
- 初学Python的学习笔记5----map()/reduce()、filter()
- Python进阶:函数式编程(高阶函数,map,reduce,filter,sorted,返回函数,匿名函数,偏函数)...啊啊啊
- 王亟亟的Python学习之路(八)-函数式编程,map(),reduce(),filter()
- Python教程学习简记8--Python 高阶函数 map/reduce filter sorted
- python学习代码笔记(map,reduce,filter,sort,decorator,functools.partial)
- Python学习(八)——map、reduce、filter、sorted
- Python学习笔记(5):lambda,filter,map,reduce,zip用法整理