Python入门篇-高阶函数

              Python入门篇-高阶函数

                                      作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

一.高级函数 

1>.First Class Object

    函数在Python中是一等公民
    函数也是对象,可调用的对象
    函数可以作为普通变量,参数,返回值等等

2>.高阶函数

    数学概念:y=g(f(x))
    在数学和计算机科学中,高阶函数应当是至少满足下面一条条件的函数
        接收一个或多个函数作为参数
        输出一个函数对象

3>.计数器

 1 #!/usr/bin/env python
 2 #_*_coding:utf-8_*_
 3 #@author :yinzhengjie
 4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
 5 #EMAIL:y1053419035@qq.com
 6 
 7 def counter(base):
 8     def inc(step=1):
 9         nonlocal base       #这里声明base不在inc作用域内(是counter内部作用域中的一个变量),当腰使用base可以去它上级作用域查找,但是不能去全局作用域查找哟~
10         base += step
11         return base
12     return inc
13 
14 f1 = counter(5)
15 
16 f2 = counter(5)
17 
18 print("id(f1) = {} ,id(f2) = {} ,{}".format(id(f1),id(f2),f1 == f2))
19 
20 print("f1() = {}".format(f1()))
21 print("f2() = {}".format(f2()))
22 
23 
24 
25 #以上代码执行结果如下:
26 id(f1) = 42081272 ,id(f2) = 42081408 ,False
27 f1() = 6
28 f2() = 6

二.自定义sort函数

1>.排序问题

排序问题:
  仿照内奸函数sorted,请自行实现一个sort函数(不使用内建函数)

思路:
  内建函数sorted函数是返回一个新的列表,可以设置升序或降序,可以设置一个排序的函数。内自定义sort函数也要实现这个功能。
  新建一个列表,遍历原列表,和新列表的值依次比较决定如何插入到新列表中。  
思考:
  sorted函数的实现原理,扩展到map,filter函数的实现原理。

2>.sort函数实现

 1 #!/usr/bin/env python
 2 #_*_coding:utf-8_*_
 3 #@author :yinzhengjie
 4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
 5 #EMAIL:y1053419035@qq.com
 6 
 7 def sort(iterable,reverse = False):
 8     list_1 = []
 9     for x in iterable:
10         for index, y in enumerate(list_1):
11             flag = x > y if reverse else x < y
12             if flag:                        # 找到大的就地插入。如果换成x < y呢,函数什么意思呢?
13                 list_1.insert(index,x)      # 降序
14                 break
15         else:                               # 不大于,说明是最小的,尾部追加
16                 list_1.append(x)
17     return list_1
18 
19 src = [1,2,5,4,2,3,5,6]
20 
21 dest = sort(src)
22 
23 print("src = {}".format(src))
24 print("dest = {}".format(dest))
25 print("dest = {}".format(sort(src,False)))
26 print("dest = {}".format(sort(src,True)))
27 
28 
29 
30 #以上代码执行结果如下:
31 src = [1, 2, 5, 4, 2, 3, 5, 6]
32 dest = [1, 2, 2, 3, 4, 5, 5, 6]
33 dest = [1, 2, 2, 3, 4, 5, 5, 6]
34 dest = [6, 5, 5, 4, 3, 2, 2, 1]
版本一
#!/usr/bin/env python
#_*_coding:utf-8_*_
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
#EMAIL:y1053419035@qq.com

def comp(a,b,reverse):
    return  a > b if reverse else a < b

def sort(iterable,reverse = False):
    list_1 = []
    for x in iterable:
        for index, y in enumerate(list_1):
            if comp(x,y,reverse):                        # 找到大的就地插入。如果换成x < y呢,函数什么意思呢?
                list_1.insert(index,x)      # 降序
                break
        else:                               # 不大于,说明是最小的,尾部追加
                list_1.append(x)
    return list_1

src = [1,2,5,4,2,3,5,6]

dest = sort(src)

print("src = {}".format(src))
print("dest = {}".format(dest))
print("dest = {}".format(sort(src,False)))
print("dest = {}".format(sort(src,True)))



#以上代码执行结果如下:
src = [1, 2, 5, 4, 2, 3, 5, 6]
dest = [1, 2, 2, 3, 4, 5, 5, 6]
dest = [1, 2, 2, 3, 4, 5, 5, 6]
dest = [6, 5, 5, 4, 3, 2, 2, 1]
版本二
 1 #!/usr/bin/env python
 2 #_*_coding:utf-8_*_
 3 #@author :yinzhengjie
 4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
 5 #EMAIL:y1053419035@qq.com
 6 
 7 
 8 def sort(iterable,key = lambda a,b:a<b,reverse = False):
 9     list_1 = []
10     for x in iterable:
11         for index, y in enumerate(list_1):
12             flag = key(x,y) if reverse else key(y,x)
13             if flag:                        # 找到大的就地插入。如果换成x < y呢,函数什么意思呢?
14                 list_1.insert(index,x)      # 降序
15                 break
16         else:                               # 不大于,说明是最小的,尾部追加
17                 list_1.append(x)
18     return list_1
19 
20 src = [1,2,5,4,2,3,5,6]
21 
22 dest = sort(src)
23 dest2 = sort(src,reverse=True)
24 
25 print("src = {}".format(src))
26 print("dest = {}".format(dest))
27 print("dest2 = {}".format(dest2))
28 
29 
30 
31 
32 #以上代码执行结果如下:
33 src = [1, 2, 5, 4, 2, 3, 5, 6]
34 dest = [6, 5, 5, 4, 3, 2, 2, 1]
35 dest2 = [1, 2, 2, 3, 4, 5, 5, 6]

三.内建函数-高阶函数

1>.sorted(iterable[, key][, reverse]) 排序

 1 #!/usr/bin/env python
 2 #_*_coding:utf-8_*_
 3 #@author :yinzhengjie
 4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
 5 #EMAIL:y1053419035@qq.com
 6 
 7 
 8 
 9 """
10 sorted(iterable[, key][, reverse]) 排序
11     返回一个新的列表,对一个可迭代对象的所有元素排序,排序规则为key定义的函数,reverse表示是否排序翻转
12 """
13 
14 src = [1, 2, 5, 4, 2, 3, 5, 6]
15 
16 # 返回新列表
17 dest = sorted(src,key=lambda x:6-x)
18 dest2 = sorted(src,key=lambda x:6-x,reverse=True)
19 
20 print("src = {}".format(src))
21 print("dest = {}".format(dest))
22 print("dest2 = {}".format(dest2))
23 
24 
25 
26 #以上代码执行结果如下:
27 src = [1, 2, 5, 4, 2, 3, 5, 6]
28 dest = [6, 5, 5, 4, 3, 2, 2, 1]
29 dest2 = [1, 2, 2, 3, 4, 5, 5, 6]

2>.filter(function, iterable) 过滤

 1 #!/usr/bin/env python
 2 #_*_coding:utf-8_*_
 3 #@author :yinzhengjie
 4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
 5 #EMAIL:y1053419035@qq.com
 6 
 7 """
 8 filter(function, iterable)
 9     过滤可迭代对象的元素,返回一个迭代器
10     function一个具有一个参数的函数,返回bool
11 """
12 
13 src = [1,9,55,150,-3,78,28,123]
14 
15 #例如,过滤出数列中能被3整除的数字
16 dest = list(filter(lambda x: x%3==0,src))
17 
18 
19 print("src = {}".format(src))
20 print("dest = {}".format(dest))
21 
22 
23 
24 #以上代码执行结果如下:
25 src = [1, 9, 55, 150, -3, 78, 28, 123]
26 dest = [9, 150, -3, 78, 123]

3>. map(function, *iterables) --> map object

 1 #!/usr/bin/env python
 2 #_*_coding:utf-8_*_
 3 #@author :yinzhengjie
 4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
 5 #EMAIL:y1053419035@qq.com
 6 
 7 
 8 """
 9 map(function, *iterables) --> map object
10     对多个可迭代对象的元素按照指定的函数进行映射,返回一个迭代器
11 """
12 
13 
14 print(list(map(lambda x:2*x+1, range(5))))
15 
16 print(dict(map(lambda x:(x%5,x) , range(500))))
17 
18 
19 
20 #以上代码执行结果如下:
21 [1, 3, 5, 7, 9]
22 {0: 495, 1: 496, 2: 497, 3: 498, 4: 499}

四.柯里化Curing

1>.柯里化概述

  指的是将原来接受两个参数的函数变成新的接受一个参数的函数的过程。新的函数返回一个以原有第二个参数为参数的函数

  z
= f(x, y) 转换成z = f(x)(y)的形式

2>.通过嵌套函数就可以把函数转换成柯里化函数

 1 #!/usr/bin/env python
 2 #_*_coding:utf-8_*_
 3 #@author :yinzhengjie
 4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/
 5 #EMAIL:y1053419035@qq.com
 6 
 7 def add(x, y):
 8     return x + y
 9 
10 
11 #将加法函数柯里化,转换如下,即:通过嵌套函数就可以把函数转换成柯里化函数
12 def add2(x):
13     def _add(y):
14         return x+y
15     return _add
16 
17 print(add(10,20))           #普通函数
18 
19 print(add2(10)(20))         #柯里化函数
20 
21 
22 
23 #以上代码执行结果如下:
24 30
25 30
原文地址:https://www.cnblogs.com/yinzhengjie/p/10963741.html