1. 程式人生 > >函數作用域,匿名函數,map,filter,reduce---Python重新開始第五天

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

匿名 留下 pytho log one super fun nbsp article

函數作用域

函數的作用域只跟函數聲明時定義的作用域有關,跟函數的調用位置無任何關系

 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)))

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()

處理一個序列,然後把序列進行合並操作

 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))

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