First, the anonymous function : also known as lambda expressions
1. Anonymous core functions: some simple functions needed to solve the problem, the function body of the anonymous function is only one line
2. There may be multiple parameters, separated by commas
3. The return value of function as normal data may be any type of
Second, the anonymous function exercises
1 Please converts the following function into anonymous function 2 DEF the Add (X, Y) . 3 return X + Y . 4 the Add () . 5
Results: . 6 SUM1 = the lambda X, Y: X + Y . 7 Print (SUM1 ( . 5 , . 8 ))
. 1 DIC = { ' K1 ' : 50 , ' K2 ' : 80 , ' K3 ' : 90 } 2 # FUNC = the lambda K: DIC [K] . 3 # Print (max (DIC, Key = FUNC)) . 4 Print (max (dic, key = lambda k: dic [k])) # is equivalent to the following one the above two
1 3.map方法 2 l=[1,2,3,4] 3 # def func(x): 4 # return x*x 5 # print(list(map(func,l))) 6 7 print(list(map(lambda x:x*x,l)))
1 l=[15,24,31,14] 2 # def func(x): 3 # return x>20 4 # print(list(filter(func,l))) 5 6 print(list(filter(lambda x:x>20,l)))
1 # method 2 T1 = (( ' A ' ), ( ' B ' )) . 3 T2 = (( ' C ' ), ( ' D ' )) . 4 # Print (List (ZIP (T1, T2))) . 5 Print (List (Map (the lambda T: {T [ 0 ], T [ . 1 ]}, ZIP (T1, T2)))) . 6 . 7 # method II . 8 Print (List ([{I, J} for I, J in ZIP (T1, T2)])) . 9 10 # method three . 11 FUNC the lambda = T1, T2: [{I, J} for I, J inzip (t1, t2)] 12 right = func (T1, T2) 13 printer (right)
Third, list comprehensions
. 1 . 6 within 3 .30 all divisible 2 Print (List ([I for I in Range (30) IF I% 3 == 0]))
Third, the dictionary down style
Example: The key and value a dictionary swap
1 mcase = {'a': 10, 'b': 34} 2 res1 = {i:mcase[i] for i in mcase} 3 res={mcase[i]:i for i in mcase } 4 print(res1) 5 print(res)
Two cases: case combined value corresponding to the value of k uniform lowercase
1 mcase = {'a':10,'b':34,'A':7} 2 res = {i.lower():mcase.get(i.lower(),0)+mcase.get(i.upper(),0) for i in mcase} 3 print(res)
Fourth, the set down style
Example: calculating the square of each value in the list, the weight comes to function
1 l=[5,-5,1,2,5] 2 print({i**2 for i in l})
First, the anonymous function : also known as lambda expressions
1. Anonymous core functions: some simple functions needed to solve the problem, the function body of the anonymous function is only one line
2. There may be multiple parameters, separated by commas
3. The return value of function as normal data may be any type of
Second, the anonymous function exercises
1 请把下面的函数转换成匿名函数 2 def add(x,y) 3 return x+y 4 add() 5
结果: 6 sum1=lambda x,y:x+y 7 print(sum1(5,8))
1 dic = {'k1':50,'k2':80,'k3':90} 2 # func= lambda k:dic[k] 3 # print(max(dic,key=func)) 4 print(max(dic,key = lambda k:dic[k]))#上面两句就相当于下面一句
1 3.map方法 2 l=[1,2,3,4] 3 # def func(x): 4 # return x*x 5 # print(list(map(func,l))) 6 7 print(list(map(lambda x:x*x,l)))
1 l=[15,24,31,14] 2 # def func(x): 3 # return x>20 4 # print(list(filter(func,l))) 5 6 print(list(filter(lambda x:x>20,l)))
1 # 方法一 2 t1=(('a'),('b')) 3 t2=(('c'),('d')) 4 # print(list(zip(t1,t2))) 5 print(list(map(lambda t:{t[0],t[1]},zip(t1,t2)))) 6 7 # 方法二 8 print(list([{i,j} for i,j in zip(t1,t2)])) 9 10 #方法三 11 func = lambda t1,t2:[{i,j} for i,j in zip(t1,t2)] 12 ret = func(t1,t2) 13 print(ret)
三、列表推导式
1 6.30以内所有被3整除的数 2 print(list([i for i in range(30) if i%3==0]))
三、字典推倒式
例一:将一个字典的key和value对调
1 mcase = {'a': 10, 'b': 34} 2 res1 = {i:mcase[i] for i in mcase} 3 res={mcase[i]:i for i in mcase } 4 print(res1) 5 print(res)
例二:合并大小写对应的value值,将k统一成小写
1 mcase = {'a':10,'b':34,'A':7} 2 res = {i.lower():mcase.get(i.lower(),0)+mcase.get(i.upper(),0) for i in mcase} 3 print(res)
Fourth, the set down style
Example: calculating the square of each value in the list, the weight comes to function
1 l=[5,-5,1,2,5] 2 print({i**2 for i in l})