Some small details of a Python lambda expression

Source: Internet
Author: User

Warm so know new, inadvertently found before experiment Lambda writing test code, the first reflection is, this is I write????!!

Hehe, think of xx language just put Lambda formally add in, Python early support, I can yell "Python is the best language" to find scold it?

Ha ha.

However, since having lambda, a lot of code has been done in one line. However, there are a lot of small details not for the attention of the general people, see the following code:



# coding=utf-8# Gabriel G=lambda x,y:x+ytotal=0;for i in range (1,101):      #print  i    total=g (total,i)     print total# Even if you define the same lambda method, they are not the same g1=lambda x,y:x+yg2=lambda x,y:x+yprint g1print id (G1) print  G2print id (G2) #lambda方法嵌套定义, the lambda method does not apply a variable reference and calls print  directly (lambda x,y:x+ ((lambda a:a+1) (y))) ( #lambda方法定义不可以回车, which means that code like for this cannot appear in the lambda method #error# lambda x,y:#     num =0#     for i in range (x,y+1) #          num+=i# all the values in the sequence parameters that meet the true criteria in the iteration are the # tuple and list Print filter (None, (1,0,true,false)) Print filter ( None,[1,0,true,false]) #字典的奇葩结果: Returns a list of keys and evaluates to the type of the first conforming element as the Datum print filter (none,{"a": 1, "B": 0, "C": True, "D ": false}") Print filter (none,{1: ' s ', 0: ' s ', True: ' s ', false: ' s '}) Print filter (none,{true: ' s ', false: ' s ', 1: ' s ', 0: ' s '}) Print filter (NOne,{0: ' s ', 1: ' s ', true: ' s ', False: ' s '} ' Print filter (none,{false: ' s ', 0: ' s ', 1: ' s ', true: ' s '}) print filter (none,{false: ' s ', 0: ' s ', 1: ' s ', True: ' s ', 2: ' s ', 1.23: ' s ', ' KK ': ' s ', 0.00: ' s '}) Print filter (lambda x:x%2,{1 : ' s ', 2: ' s ', 3: ' s ', 4: ' s '}) #取反print  filter (lambda x:not x,[1,0,true,false] ) #取奇数和偶数print  filter (lambda x:x%2, [1,2,3,4,5,6,7]) Print filter (lambda x: (x+1)%2, [ 1,2,3,4,5,6,7]) #print  filter (Lambda [x,y]:(x+y)%2, [[1,2],[3,4],[5,6],[7,1]) errorprint  Map (None,range) Print map (Lambda x:x*100,range (Ten)) Print map (lambda x:x*100+ (x%2) * (-1) + ((x+1)%2) *1,range (Ten) Print map (Lambda x:0,range ()) Print map (Lambda x:[x-1,x,x+1],range ( ) Print map (lambda x: (x-1,x,x+1), Range (Ten)) #元组会变成元素相同的列表print  map (None, (1,2,3,4,5)) #即使返回的列表相同 , the address is different, that is, two lists are not the same l1 = [1, 2, 3, 4, 5, 6]l2 = map (NONE, L1 ) Print l1,id (L1) print&Nbsp;l2,id (L2) 

The

Output results are as follows: (I will insert the output counterpart in the time of the day, and now please make sure to see it)

5050<function <lambda> at 0xb7487bfc>3074980860<function <lambda>  at 0xb7487f7c>30749817564 (1, true) [1, true][' A ',  ' C ',  ' B ',  ' d '][1][True ][1][1][1, 2,  ' KK ', 1.23][1, 3][0, false][1, 3, 5, 7][2, 4,  6][0,&NBSP;1,&NBSP;2,&NBSP;3,&NBSP;4,&NBSP;5,&NBSP;6,&NBSP;7,&NBSP;8,&NBSP;9][0,&NBSP;100,&NBSP;200,&NBSP;300,  400, 500, 600, 700, 800, 900][1, 99, 201, 299, 401,  499, 601, 699, 801, 899][0, 0, 0, 0, 0, 0, 0, 0, 0 ,  0][[-1, 0, 1], [0, 1, 2], [1, 2, 3], [2, 3, 4],  [3, 4, 5], [4, 5, 6], [5, 6, 7], [6, 7, 8],  [7, 8, 9], [8, 9, 10]][( -1, 0, 1),  (0, 1, 2),  (1,  2,&NBSP;3), (2, 3, 4),  (3, 4, 5),  (4, 5, 6),  (5, 6, 7),  (6,  7, 8),  (7, 8, 9),  (8, 9, 10)][1, 2, 3, 4, 5][1,  2, 3, 4, 5, 6] 3074533388[1, 2, 3, 4, 5, 6]  3074983436


Some small details of a Python lambda expression

Related Article

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.