anonymous function-lambda

Source: Internet
Author: User

anonymous function-lambda

The anonymous function lambda [arg1 [, arg2,..... argn]]:expression Lambda is just an expression, and the function body is much simpler than def. The body of a lambda is an expression, not a block of code.    Only a finite amount of logic can be encapsulated in a lambda expression.    The lambda function has its own namespace and cannot access parameters outside its own argument list or in the global namespace.    Although the lambda function appears to be only a single line, it is not equivalent to a C or C + + inline function, which is designed to call small functions without consuming stack memory to increase operational efficiency.     Exp:dic = {' K1 ': Ten, ' K2 ': +, ' K3 ': +, ' K4 ': +} print (max (Dic,key=lambda K:dic[k])) The built-in function has the following key parameters:    Max min Filter map sorted above can be combined with lambda to complete some functions exp2: Existing two tuple tup1,tup2, use anonymous function production list lst Tup1 = ((' A '), (' B ')) Tup2 = ((' C '), (' d ')) LST = [{' A ': ' C '},{' B ': ' d '}] 1: LST written by yourself = lambda x, y: [{(x[i][0]): y[i][0]} for I in rang E (len (x))] Print (LST (tup1,tup2)) [{' A ': ' C '}, {' B ': ' d '}] notation 2:tup1 = ((' A '), (' B ')) Tup2 = ((' C '), (' d ') # lst = [{' A ': ' C '},{' B ': ' d '}] # ret = Zip (tup1,tup2) # def func (t): # return {t[0]:t[1]} # func = Lam BDA T:{t[0]:t[1]} # res = map (lambda t:{t[0]:t[1]},zip (tup1,tup2)) Print (list (map (Lambda T:{t[0]:t[1]},zip (tup1,tup2 ))) Exp3. The following codeWhat is the output?    Please give an answer and explain. def multipliers (): return [lambda x:i*x for I in range (4)] Print ([M (2) for M in Multipliers ()]) please modify the multiplier    s is defined to produce the desired result. Resolution: Def multipliers (): return [lambda x:i *x for I in range (4)] #返回列表推导式 [Lambda x:i *x,lambda x: I *x,lambda x:i *x,lambda x:i *x] Print ([M (2) for M in Multipliers ()]) # [M (2) to M in [Lambda x:i *x , Lambda x:i *x,lambda x:i *x,lambda x:i *x]] # MULTIPLIERSW () for [Lambda x:i *x,lambda x:i *x,lambda x:i *  X,lambda x:i *x] # [M (2) for M in [Lambda x:i *x,lambda x:i *x,lambda x:i *x,lambda x:i *x]] # m is Lambda X:i *m, 4 times # m (2) [Lambda x:i *2,lambda x:i *2,lambda x:i *2,lambda x:i]] #而此时multip The For Loop in Liers () is finished, and the last value of I is 3 # so [Lambda x:3 *2,lambda x:3 *2,lambda x:3 *2,lambda x:3 *]] #[6, 6, 6 , 6] exp4. What is the output of the following code?        Please give an answer and explain. def multipliers (): Return (lambda x:i *x for I in range (4)) #返回一个生成器 g = (lambda x:i *x for I in range (4) print ([M (2) to M in Multipliers ()]) # [M (2)-M in Mu  Ltipliers ()] # [M (2) for M in (Lambda x:i *x-I in range (4)] # [M (2) for M in (Lambda x:0 *x,lambda x : 1 *x,lambda x:2 *x,lambda x:3 *x] # [2 * 0,,2 * 1, 2 * 2,2 * 3] #[0,2,4,6]

  

anonymous function-lambda

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.