VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > Python基础教程 >
  • python基础教程之day16-python之函数式编程匿名函数

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':'