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.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]

版本二

 #!/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入门篇-高阶函数的更多相关文章

  1. Python 函数式编程 & Python中的高阶函数map reduce filter 和sorted

    1. 函数式编程 1)概念 函数式编程是一种编程模型,他将计算机运算看做是数学中函数的计算,并且避免了状态以及变量的概念.wiki 我们知道,对象是面向对象的第一型,那么函数式编程也是一样,函数是函数 ...

  2. Python中的高阶函数与匿名函数

    Python中的高阶函数与匿名函数 高阶函数 高阶函数就是把函数当做参数传递的一种函数.其与C#中的委托有点相似,个人认为. def add(x,y,f): return f( x)+ f( y) p ...

  3. Python学习笔记 - 高阶函数

    高阶函数英文叫Higher-order function.什么是高阶函数?我们以实际代码为例子,一步一步深入概念. 变量可以指向函数 以Python内置的求绝对值的函数abs()为例,调用该函数用以下 ...

  4. 匿名函数python内置高阶函数以及递归

    匿名函数 python定义一个函数通常使用def关键词,后面跟函数名,然后是注释.代码块等. def func(): '''注释''' print('from func') 这样就在全局命名空间定义了 ...

  5. python 常用的高阶函数

    前言 高阶函数指的是能接收函数作为参数的函数或类:python中有一些内置的高阶函数,在某些场合使用可以提高代码的效率. map() map函数可以把一个迭代对象转换成另一个可迭代对象,不过在pyth ...

  6. python学习笔记——高阶函数map()

    满足以下两点中任意一点,即为高阶函数: 1.函数接收一个或多个函数作为参数 2.函数返回一个函数 1 描述 用函数和可迭代对象中每一个元素作为参数,计算出新的迭代对象 map() 会根据提供的函数对指 ...

  7. python中的高阶函数

    高阶函数英文叫Higher-order function.什么是高阶函数?我们以实际代码为例子,一步一步深入概念. 变量可以指向函数 以Python内置的求绝对值的函数abs()为例,调用该函数用以下 ...

  8. Python高级教程-高阶函数

    Higher-order function(高阶函数) 变量可以指向函数 以Python内置的求绝对值的函数abs()为例,调用该函数用以下代码: >>> abs(-10) 10 但 ...

  9. python字符串反转 高阶函数 @property与sorted(八)

    (1)字符串反转 1倒序输出 s = 'abcde' print(s[::-1]) #输出: 'edcba' 2 列表reverse()操作 s = 'abcde' lt = list(s) lt.r ...

随机推荐

  1. jzy3D从入门到弃坑_4尝试使用jzy3D1.0画图失败

    jzy3D从入门到弃坑_4 尝试使用jzy3D1.0画图失败 觉得有用的话,欢迎一起讨论相互学习~Follow Me 记录一下使用jzy3D1.0失败 究其原因在于 本人才疏学浅,对于JAVA ope ...

  2. Docker管理控制相关资源

    一台宿主机可以放多个容器,默认的情况下,Docker 没有对容器进行硬件资源的限制,当容器负载过高时会尽可能的占用宿主机资源,所以有时候我们需要对容器的资源使用设置一个上限,这里就需要管理 Docke ...

  3. echarts柱状图坐标文字显示不完整解决方式

    echarts柱状图坐标文字显示不完整解决方式 本文转载自:https://jingyan.baidu.com/article/ab69b2707a9aeb2ca7189f0c.html echart ...

  4. LeetCode176——第二高的薪水

    题目描述 编写一个 SQL 查询,获取 Employee 表中第二高的薪水(Salary) . +----+--------+ | Id | Salary | +----+--------+ | 1 ...

  5. [EXP]CVE-2019-9621 Zimbra<8.8.11 GetShell Exploit(配合Cscan可批量)

    发现时间 2019年03月18日 威胁目标 采用Zimbra邮件系统的企业 主要风险 远程代码执行 攻击入口 localconfig.xml  配置文件 使用漏洞 CVE-2019-9621 受影响应 ...

  6. 【Uiautomatorviewer】报错:Unexpected error while obtaining UI hierarchy java.lang.reflect.InvocationT...

    android 9.0系统不能用uiautomator识别 解决方法:android 8.0 以后 uiautomator 无法直接使用的问题https://www.cnblogs.com/copyw ...

  7. 用NDK生成cURL和OpenSSL库

    最近在用Qt开发Android应用时需要获取https页面内容,但Qt内置的QNetworkAccessManager类只支持下面这些协议(调用其supportedSchemes成员函数获取): (& ...

  8. Docker容器跨主机通信之:OVS+GRE

    一.概述 由于docker自身还未支持跨主机容器通信,需要借助docker网络开源解决方案 OVS OpenVSwich即开放式虚拟交换机实现,简称OVS,OVS在云计算领域应用广泛,值得我们去学习使 ...

  9. linux tomcat开机自启/nginx开机自启

    修改/etc/rc.d/rc.local文件,修改完成后需执行以下指令才能正常自启动 chmod +x /etc/rc.d/rc.local #!/bin/bash # THIS FILE IS AD ...

  10. 解决fiddler不能抓取firefox浏览器包的问题(转)

    转自:https://blog.csdn.net/jimmyandrushking/article/details/80819103