函数作用域,匿名函数,map,filter,reduce---Python重新开始第五天

函数作用域

函数的作用域只跟函数声明时定义的作用域有关,跟函数的调用位置无任何关系

 1 name='alex'
 2 
 3 def foo():
 4     name='lhf'
 5     def bar():
 6         name='wupeiqi'
 7         print(name)
 8         def tt():
 9             print(name)
10         return tt
11     return bar
12 
13 # bar=foo()
14 # tt=bar()
15 # print(tt)
16 # tt()
17 r1 = foo()
18 r2 = r1()  # tt
19 r3 = r2()
20 foo()()()
 1 高阶函数(满足一个条件)
 2 1.函数接收的参数是一个函数名  2.返回值中包含函数
 3 把函数当作参数传给另外一个函数
 4 def foo(n): #n=bar
 5     print(n)
 6 
 7 def bar(name):
 8     print('my name is %s' %name)
 9 
10 # foo(bar)
11 # foo(bar())
12 foo(bar('alex'))
13 
14 #返回值中包含函数
15 def bar():
16     print('from bar')
17 def foo():
18     print('from foo')
19     return bar
20 n=foo()
21 n()
22 def hanle():
23     print('from handle')
24     return hanle
25 h=hanle()
26 h()
27 
28 
29 
30 def test1():
31     print('from test1')
32 def test2():
33     print('from handle')
34     return test1()

尾调用:https://blog.csdn.net/wusecaiyun/article/details/46531891

在递归函数的最后一步return自身(),会直接调到下一层函数,因为如果是return x +函数(),

那么这个x+ 会一直等着函数()执行返回的结果。如果是尾调用,直接进函数,没有任何res在

等待函数()返回值

匿名函数lambda

 1 # lambda x:x+1
 2 
 3 
 4 def calc(x):
 5     return x+1
 6 
 7 res=calc(10)
 8 print(res)
 9 print(calc)
10 
11 print(lambda x:x+1)
12 func=lambda x:x+1
13 print(func(10))
14 
15 
16 
17 
18 name='alex' #name='alex_sb'
19 def change_name(x):
20     return name+'_sb'
21 
22 res=change_name(name)
23 print(res)
24 
25 func=lambda x:x+'_sb'
26 res=func(name)
27 print('匿名函数的运行结果',res)
28 
29 
30 # func=lambda x,y,z:x+y+z
31 # print(func(1,2,3))
32 
33 name1='alex'
34 name2='sbalex'
35 name1='supersbalex'
36 
37 
38 
39 # def test(x,y,z):
40 #     return x+1,y+1  #----->(x+1,y+1)
41 
42 # lambda x,y,z:(x+1,y+1,z+1)

map()

匿名函数可以与map,filter,reduce结合使用,精简代码

map处理的是一个可迭代对象,内部用for遍历可迭代对象的每一条数据,数据被传入的函数处理,

得到的结果也是一个可迭代对象,用list处理,得到列表,并且该‘列表’元素个数及位置与原来一样

 1 # num_l=[1,2,10,5,3,7]
 2 # num1_l=[1,2,10,5,3,7]
 3 
 4 # ret=[]
 5 # for i in num_l:
 6 #     ret.append(i**2)
 7 #
 8 # print(ret)
 9 
10 # def map_test(array):
11 #     ret=[]
12 #     for i in num_l:
13 #         ret.append(i**2)
14 #     return ret
15 #
16 # ret=map_test(num_l)
17 # rett=map_test(num1_l)
18 # print(ret)
19 # print(rett)
20 
21 num_l=[1,2,10,5,3,7]
22 #lambda x:x+1
23 def add_one(x):
24     return x+1
25 
26 #lambda x:x-1
27 def reduce_one(x):
28     return x-1
29 
30 #lambda x:x**2
31 def pf(x):
32     return x**2
33 
34 def map_test(func,array):
35     ret=[]
36     for i in num_l:
37         res=func(i) #add_one(i)
38         ret.append(res)
39     return ret
40 # print(map_test(add_one,num_l))
41 # print(map_test(lambda x:x+1,num_l))
42 
43 # print(map_test(reduce_one,num_l))
44 # print(map_test(lambda x:x-1,num_l))
45 
46 # print(map_test(pf,num_l))
47 # print(map_test(lambda x:x**2,num_l))
48 
49 
50 
51 #终极版本
52 def map_test(func,array): #func=lambda x:x+1    arrary=[1,2,10,5,3,7]
53     ret=[]
54     for i in array:
55         res=func(i) #add_one(i)
56         ret.append(res)
57     return ret
58 
59 print(map_test(lambda x:x+1,num_l))
60 res=map(lambda x:x+1,num_l)
61 print('内置函数map,处理结果',res)
62 # for i in res:
63 #     print(i)
64 print(list(res))
65 print('传的是有名函数',list(map(reduce_one,num_l)))
66 
67 
68 msg='linhaifeng'
69 print(list(map(lambda x:x.upper(),msg)))
改成字符串
l = [1,2,3,4,5] print(list(map(str, l)))

filter()  #过滤

filter处理的是一个可迭代对象,内部用for遍历每一条数据,被传入的函数判断出布尔值,如果是True则留下来,如果不是就被丢弃,最终得到的结果也是一个可迭代对象,被list处理后得到列表

 1 # movie_people=['sb_alex','sb_wupeiqi','linhaifeng','sb_yuanhao']
 2 # def filter_test(array):
 3 #     ret=[]
 4 #     for p in array:
 5 #         if not p.startswith('sb'):
 6 #                ret.append(p)
 7 #     return ret
 8 #
 9 # res=filter_test(movie_people)
10 # print(res)
11 
12 
13 # movie_people=['alex_sb','wupeiqi_sb','linhaifeng','yuanhao_sb']
14 # def sb_show(n):
15 #     return n.endswith('sb')
16 #
17 # def filter_test(func,array):
18 #     ret=[]
19 #     for p in array:
20 #         if not func(p):
21 #                ret.append(p)
22 #     return ret
23 #
24 # res=filter_test(sb_show,movie_people)
25 # print(res)
26 
27 #终极版本
28 movie_people=['alex_sb','wupeiqi_sb','linhaifeng','yuanhao_sb']
29 # def sb_show(n):
30 #     return n.endswith('sb')
31 #--->lambda n:n.endswith('sb')
32 
33 def filter_test(func,array):
34     ret=[]
35     for p in array:
36         if not func(p):
37                ret.append(p)
38     return ret
39 
40 res=filter_test(lambda n:n.endswith('sb'),movie_people)
41 print(res)
42 
43 #filter函数
44 movie_people=['alex_sb','wupeiqi_sb','linhaifeng','yuanhao_sb']
45 print(filter(lambda n:not n.endswith('sb'),movie_people))
46 
47 
48 
49 res=filter(lambda n:not n.endswith('sb'),movie_people)
50 print(list(res))
51 
52 
53 print(list(filter(lambda n:not n.endswith('sb'),movie_people)))

reduce() 

注意:要导入 from functools import reduce

处理一个序列,然后把序列进行合并操作

 1 # from functools import reduce
 2 
 3 
 4 # num_l=[1,2,3,100]
 5 
 6 # res=0
 7 # for num in num_l:
 8 #     res+=num
 9 #
10 # print(res)
11 
12 # num_l=[1,2,3,100]
13 # def reduce_test(array):
14 #     res=0
15 #     for num in array:
16 #         res+=num
17 #     return res
18 #
19 # print(reduce_test(num_l))
20 
21 
22 # num_l=[1,2,3,100]
23 
24 # def multi(x,y):
25 #     return x*y
26 #lambda x,y:x*y
27 
28 # def reduce_test(func,array):
29 #     res=array.pop(0)
30 #     for num in array:
31 #         res=func(res,num)
32 #     return res
33 #
34 # print(reduce_test(lambda x,y:x*y,num_l))
35 
36 num_l=[1,2,3,100]
37 def reduce_test(func,array,init=None):
38     if init is None:
39         res=array.pop(0)
40     else:
41         res=init
42     for num in array:
43         res=func(res,num)
44     return res
45 
46 print(reduce_test(lambda x,y:x*y,num_l,100))
47 
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))
1 from functools import reduce
2 #求和
3 l = [1,2,3,4,5]
4 
5 print(reduce(lambda x,y:x+y ,l))
原文地址:https://www.cnblogs.com/lishuaing/p/10687014.html