首页 技术 正文
技术 2022年11月15日
0 收藏 679 点赞 2,935 浏览 5170 个字

              Python入门篇-高阶函数

                                      作者:尹正杰

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

一.高级函数 

1>.First Class Object

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

2>.高阶函数

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

3>.计数器

 #!/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 counter(base):
def inc(step=1):
nonlocal base #这里声明base不在inc作用域内(是counter内部作用域中的一个变量),当腰使用base可以去它上级作用域查找,但是不能去全局作用域查找哟~
base += step
return base
return inc f1 = counter(5) f2 = counter(5) print("id(f1) = {} ,id(f2) = {} ,{}".format(id(f1),id(f2),f1 == f2)) print("f1() = {}".format(f1()))
print("f2() = {}".format(f2())) #以上代码执行结果如下:
id(f1) = 42081272 ,id(f2) = 42081408 ,False
f1() = 6
f2() = 6

二.自定义sort函数

1>.排序问题

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

2>.sort函数实现

 #!/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 sort(iterable,reverse = False):
list_1 = []
for x in iterable:
for index, y in enumerate(list_1):
flag = x > y if reverse else x < y
if flag: # 找到大的就地插入。如果换成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]

版本一

#!/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.comdef comp(a,b,reverse):
return a > b if reverse else a < bdef 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_1src = [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]

版本二

 #!/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 sort(iterable,key = lambda a,b:a<b,reverse = False):
list_1 = []
for x in iterable:
for index, y in enumerate(list_1):
flag = key(x,y) if reverse else key(y,x)
if flag: # 找到大的就地插入。如果换成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)
dest2 = sort(src,reverse=True) print("src = {}".format(src))
print("dest = {}".format(dest))
print("dest2 = {}".format(dest2)) #以上代码执行结果如下:
src = [1, 2, 5, 4, 2, 3, 5, 6]
dest = [6, 5, 5, 4, 3, 2, 2, 1]
dest2 = [1, 2, 2, 3, 4, 5, 5, 6]

三.内建函数-高阶函数

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

 #!/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 """
sorted(iterable[, key][, reverse]) 排序
返回一个新的列表,对一个可迭代对象的所有元素排序,排序规则为key定义的函数,reverse表示是否排序翻转
""" src = [1, 2, 5, 4, 2, 3, 5, 6] # 返回新列表
dest = sorted(src,key=lambda x:6-x)
dest2 = sorted(src,key=lambda x:6-x,reverse=True) print("src = {}".format(src))
print("dest = {}".format(dest))
print("dest2 = {}".format(dest2)) #以上代码执行结果如下:
src = [1, 2, 5, 4, 2, 3, 5, 6]
dest = [6, 5, 5, 4, 3, 2, 2, 1]
dest2 = [1, 2, 2, 3, 4, 5, 5, 6]

2>.filter(function, iterable) 过滤

 #!/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 """
filter(function, iterable)
过滤可迭代对象的元素,返回一个迭代器
function一个具有一个参数的函数,返回bool
""" src = [1,9,55,150,-3,78,28,123] #例如,过滤出数列中能被3整除的数字
dest = list(filter(lambda x: x%3==0,src)) print("src = {}".format(src))
print("dest = {}".format(dest)) #以上代码执行结果如下:
src = [1, 9, 55, 150, -3, 78, 28, 123]
dest = [9, 150, -3, 78, 123]

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

 #!/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 """
map(function, *iterables) --> map object
对多个可迭代对象的元素按照指定的函数进行映射,返回一个迭代器
""" print(list(map(lambda x:2*x+1, range(5)))) print(dict(map(lambda x:(x%5,x) , range(500)))) #以上代码执行结果如下:
[1, 3, 5, 7, 9]
{0: 495, 1: 496, 2: 497, 3: 498, 4: 499}

四.柯里化Curing

1>.柯里化概述

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

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

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

 #!/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 add(x, y):
return x + y #将加法函数柯里化,转换如下,即:通过嵌套函数就可以把函数转换成柯里化函数
def add2(x):
def _add(y):
return x+y
return _add print(add(10,20)) #普通函数 print(add2(10)(20)) #柯里化函数 #以上代码执行结果如下:
30
30
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,944
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,469
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,283
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,098
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,729
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,766