Python入門篇-高階函數
python
做者:尹正傑app
版權聲明:原創做品,謝絕轉載!不然將追究法律責任。ide
一.高級函數 函數
1>.First Class Objectspa
函數在Python中是一等公民
函數也是對象,可調用的對象
函數能夠做爲普通變量,參數,返回值等等
2>.高階函數3d
數學概念:y=g(f(x))
在數學和計算機科學中,高階函數應當是至少知足下面一條條件的函數
接收一個或多個函數做爲參數
輸出一個函數對象
3>.計數器code
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函數orm
1>.排序問題對象
排序問題:
仿照內奸函數sorted,請自行實現一個sort函數(不使用內建函數)
思路:
內建函數sorted函數是返回一個新的列表,能夠設置升序或降序,能夠設置一個排序的函數。內自定義sort函數也要實現這個功能。
新建一個列表,遍歷原列表,和新列表的值依次比較決定如何插入到新列表中。
思考:
sorted函數的實現原理,擴展到map,filter函數的實現原理。
2>.sort函數實現blog
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