1.複習 2.匿名函數 3.作用域 4.函數式編程 4.map函數 5.filter函數 6.reduce函數 7.小結 8.內置函數 ...
1.複習
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 name = 'alex' #name=‘lhf’ 4 def change_name(): 5 name='lhf' 6 # global name 7 # name = 'lhf' 8 # print(name) 9 # name='aaaa' #name='bbb' 10 def foo(): 11 # name = 'wu' 12 nonlocal name 13 name='bbbb' 14 print(name) 15 print(name) 16 foo() 17 print(name) 18 19 20 change_name()
2.匿名函數
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 # def calc(x): 4 # return x+1 5 6 # res=calc(10) 7 # print(res) 8 # print(calc) 9 10 # print(lambda x:x+1) 11 # func=lambda x:x+1 12 # print(func(10)) 13 14 # name='alex' #name='alex_sb' 15 # def change_name(x): 16 # return name+'_sb' 17 # 18 # res=change_name(name) 19 # print(res) 20 21 # func=lambda x:x+'_sb' 22 # res=func(name) 23 # print('匿名函數的運行結果',res) 24 25 # func=lambda x,y,z:x+y+z 26 # print(func(1,2,3)) 27 28 # name1='alex' 29 # name2='sbalex' 30 # name1='supersbalex' 31 32 33 34 # def test(x,y,z): 35 # return x+1,y+1 #----->(x+1,y+1) 36 37 # lambda x,y,z:(x+1,y+1,z+1)
3.作用域
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 # def test1(): 4 # print('in the test1') 5 # def test(): 6 # print('in the test') 7 # return test1 8 # 9 # # print(test) 10 # res=test() 11 # # print(res) 12 # print(res()) #test1() 13 14 #函數的作用域只跟函數聲明時定義的作用域有關,跟函數的調用位置無任何關係 15 # name = 'alex' 16 # def foo(): 17 # name='linhaifeng' 18 # def bar(): 19 # # name='wupeiqi' 20 # print(name) 21 # return bar 22 # a=foo() 23 # print(a) 24 # a() #bar()
4.函數式編程
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 #高階函數1。函數接收的參數是一個函數名 2#返回值中包含函數 4 # 把函數當作參數傳給另外一個函數 5 # def foo(n): #n=bar 6 # print(n) 7 # # 8 # def bar(name): 9 # print('my name is %s' %name) 10 # # 11 # # foo(bar) 12 # # foo(bar()) 13 # foo(bar('alex')) 14 # 15 #返回值中包含函數 16 # def bar(): 17 # print('from bar') 18 # def foo(): 19 # print('from foo') 20 # return bar 21 # n=foo() 22 # n() 23 # def hanle(): 24 # print('from handle') 25 # return hanle 26 # h=hanle() 27 # h() 28 # 29 # 30 # 31 # def test1(): 32 # print('from test1') 33 # def test2(): 34 # print('from handle') 35 # return test1()
4.map函數
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 # num_l=[1,2,10,5,3,7] 4 # num1_l=[1,2,10,5,3,7] 5 6 # ret=[] 7 # for i in num_l: 8 # ret.append(i**2) 9 # 10 # print(ret) 11 12 # def map_test(array): 13 # ret=[] 14 # for i in num_l: 15 # ret.append(i**2) 16 # return ret 17 # 18 # ret=map_test(num_l) 19 # rett=map_test(num1_l) 20 # print(ret) 21 # print(rett) 22 23 num_l=[1,2,10,5,3,7] 24 #lambda x:x+1 25 def add_one(x): 26 return x+1 27 28 #lambda x:x-1 29 def reduce_one(x): 30 return x-1 31 32 #lambda x:x**2 33 def pf(x): 34 return x**2 35 36 def map_test(func,array): 37 ret=[] 38 for i in num_l: 39 res=func(i) #add_one(i) 40 ret.append(res) 41 return ret 42 43 # print(map_test(add_one,num_l)) 44 # print(map_test(lambda x:x+1,num_l)) 45 46 # print(map_test(reduce_one,num_l)) 47 # print(map_test(lambda x:x-1,num_l)) 48 49 # print(map_test(pf,num_l)) 50 # print(map_test(lambda x:x**2,num_l)) 51 52 #終極版本 53 def map_test(func,array): #func=lambda x:x+1 arrary=[1,2,10,5,3,7] 54 ret=[] 55 for i in array: 56 res=func(i) #add_one(i) 57 ret.append(res) 58 return ret 59 60 # print(map_test(lambda x:x+1,num_l)) 61 res=map(lambda x:x+1,num_l) 62 print('內置函數map,處理結果',res) 63 # for i in res: 64 # print(i) 65 # print(list(res)) 66 # print('傳的是有名函數',list(map(reduce_one,num_l))) 67 68 69 msg='linhaifeng' 70 print(list(map(lambda x:x.upper(),msg)))
5.filter函數
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 movie_people=['sb_alex','sb_wupeiqi','linhaifeng','sb_yuanhao'] 4 5 6 7 8 # def filter_test(array): 9 # ret=[] 10 # for p in array: 11 # if not p.startswith('sb'): 12 # ret.append(p) 13 # return ret 14 # 15 # res=filter_test(movie_people) 16 # print(res) 17 18 # movie_people=['alex_sb','wupeiqi_sb','linhaifeng','yuanhao_sb'] 19 # def sb_show(n): 20 # return n.endswith('sb') 21 # 22 # def filter_test(func,array): 23 # ret=[] 24 # for p in array: 25 # if not func(p): 26 # ret.append(p) 27 # return ret 28 # 29 # res=filter_test(sb_show,movie_people) 30 # print(res) 31 32 #終極版本 33 movie_people=['alex_sb','wupeiqi_sb','linhaifeng','yuanhao_sb'] 34 # def sb_show(n): 35 # return n.endswith('sb') 36 #--->lambda n:n.endswith('sb') 37 38 def filter_test(func,array): 39 ret=[] 40 for p in array: 41 if not func(p): 42 ret.append(p) 43 return ret 44 45 # res=filter_test(lambda n:n.endswith('sb'),movie_people) 46 # print(res) 47 48 #filter函數 49 movie_people=['alex_sb','wupeiqi_sb','linhaifeng','yuanhao_sb'] 50 # print(list(filter(lambda n:not n.endswith('sb'),movie_people))) 51 res=filter(lambda n:not n.endswith('sb'),movie_people) 52 print(list(res)) 53 54 55 print(list(filter(lambda n:not n.endswith('sb'),movie_people)))
6.reduce函數
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 from functools import reduce 4 5 6 # num_l=[1,2,3,100] 7 # 8 # res=0 9 # for num in num_l: 10 # res+=num 11 # 12 # print(res) 13 14 # num_l=[1,2,3,100] 15 # def reduce_test(array): 16 # res=0 17 # for num in array: 18 # res+=num 19 # return res 20 # 21 # print(reduce_test(num_l)) 22 23 # num_l=[1,2,3,100] 24 # 25 # def multi(x,y): 26 # return x*y 27 # lambda x,y:x*y 28 # 29 # def reduce_test(func,array): 30 # res=array.pop(0) 31 # for num in array: 32 # res=func(res,num) 33 # return res 34 # 35 # print(reduce_test(lambda x,y:x*y,num_l)) 36 37 # num_l=[1,2,3,100] 38 # def reduce_test(func,array,init=None): 39 # if init is None: 40 # res=array.pop(0) 41 # else: 42 # res=init 43 # for num in array: 44 # res=func(res,num) 45 # return res 46 # 47 # print(reduce_test(lambda x,y:x*y,num_l,100)) 48 49 #reduce函數 50 # from functools import reduce 51 # num_l=[1,2,3,100] 52 # print(reduce(lambda x,y:x+y,num_l,1)) 53 # print(reduce(lambda x,y:x+y,num_l))
7.小結
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 #處理序列中的每個元素,得到的結果是一個‘列表’,該‘列表’元素個數及位置與原來一樣 4 # map() 5 6 #filter遍歷序列中的每個元素,判斷每個元素得到布爾值,如果是True則留下來 7 8 people=[ 9 {'name':'alex','age':1000}, 10 {'name':'wupei','age':10000}, 11 {'name':'yuanhao','age':9000}, 12 {'name':'linhaifeng','age':18}, 13 ] 14 # print(list(filter(lambda p:p['age']<=18,people))) 15 # print(list(filter(lambda p:p['age']<=18,people))) 16 17 #reduce:處理一個序列,然後把序列進行合併操作 18 from functools import reduce 19 print(reduce(lambda x,y:x+y,range(100),100)) 20 # print(reduce(lambda x,y:x+y,range(1,101)))
8.內置函數
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 # print(abs(-1)) 4 # print(abs(1)) 5 # 6 # print(all([1,2,'1'])) 7 # print(all([1,2,'1',''])) 8 # print(all('')) 9 10 # print(any([0,''])) 11 # print(any([0,'',1])) 12 13 14 # print(bin(3)) 15 16 #空,None,0的布爾值為False,其餘都為True 17 # print(bool('')) 18 # print(bool(None)) 19 # print(bool(0)) 20 21 name='你好' 22 # print(bytes(name,encoding='utf-8')) 23 # print(bytes(name,encoding='utf-8').decode('utf-8')) 24 25 # print(bytes(name,encoding='gbk')) 26 # print(bytes(name,encoding='gbk').decode('gbk')) 27 # 28 # print(bytes(name,encoding='ascii'))#ascii不能編碼中文 29 # 30 # print(chr(46)) 31 # 32 # print(dir(dict)) 33 # 34 # print(divmod(10,3)) 35 36 # dic={'name':'alex'} 37 # dic_str=str(dic) 38 # print(dic_str) 39 40 #可hash的數據類型即不可變數據類型,不可hash的數據類型即可變數據類型 41 # print(hash('12sdfdsaf3123123sdfasdfasdfasdfasdfasdfasdfasdfasfasfdasdf')) 42 # print(hash('12sdfdsaf31231asdfasdfsadfsadfasdfasdf23')) 43 # 44 name='alex' 45 # print(hash(name)) 46 # print(hash(name)) 47 # 48 # 49 # print('--->before',hash(name)) 50 # name='sb' 51 # print('=-=>after',hash(name)) 52 53 54 # print(help(all)) 55 # 56 # print(bin(10))#10進位->2進位 57 # print(hex(12))#10進位->16進位 58 # print(oct(12))#10進位->8進位 59 60 61 name='哈哈哈哈哈哈哈哈哈哈哈哈哈哈啊哈粥少陳' 62 # print(globals()) 63 # print(__file__) 64 # 65 def test(): 66 age='1111111111111111111111111111111111111111111111111111111111111' 67 # print(globals()) 68 print(locals()) 69 # 70 # test() 71 # 72 l=[1,3,100,-1,2] 73 # print(max(l)) 74 # print(min(l)) 75 76 77 78 # print(isinstance(1,int)) 79 # print(isinstance('abc',str)) 80 print(isinstance([],list)) 81 # print(isinstance({},dict)) 82 print(isinstance({1,2},set))