day16-python之函数式编程匿名函数
程序员文章站
2022-04-14 15:35:25
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))
上一篇: day15-python之变量和递归