When the company was bored to read the Python code written by predecessors, suddenly found a more fun Python expression:
1 Lambda x,y:x+y
At first glance, this should be similar to the way, the internet looked up, so I hereby summarize
Lambda: On the Code
The parameter representing the front of the colon
Represents the expression after the colon
The return value type is associated with the parameter
1>>>Lambdax:x2<function <Lambda> at 0x01b50730>#If you don't pass it, that's it.3>>>4>>>5>>> la=Lambdax:x+y6>>> La (2)7 Traceback (most recent):8File"<stdin>", Line 1,inch<module>9File"<stdin>", Line 1,inch<Lambda>TenNameerror:GlobalName'y' is notdefined One>>> A>>> ->>> la=Lambdax,y:x+y ->>> the>>> ->>> La () -3
* * Return value:
1 >>> La (1,range (5))# list 2 [0, 1, 2, 3, 4]3 >>> 4 >>>5 >>> La (1, (+))# tuple 6 (1, 2)7 >>> La (1,'xyz')# string 8' xyz'
* * can be seen from here, Lambda is very convenient. If it's a complex expression, a personal feeling is not to use lambda.
Filter
First parameter: Method
Second parameter: sequence
A list of methods for iterating over each element of a sequence
1 def fil (x): 2 ... return and x% 3! = 03... 4 >>>5 >>>6 >>> Filter (Fil,range (1,100)) 7 [1, 5, 7, one, 8,,, all,,, +, A,, a, a, a, a, a, 65, 67, 71 , 73 ,-----------9 >>>
* * If the first parameter is passed to none, only the values inside the sequence are traversed, such as:
1 >>> filter (None,range (1,100))2 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1 6, +,3 , 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 4 , A,,,,,,,,,,,,,,,,,,,,,,,,, and5 +,---------------------------------------------------------6 83, 84, 85, 86, 87, 8 8, 94, all-in-a-------------------]7 >>>
Reduce
First parameter: Method name
Second parameter: sequence
The third parameter: spare, 0 if not preached
Do not understand why take this name, please ignore my complaints, on the code to see
The return value is associated with the parameter
1>>>defAdd (x, y):2...Print 'x='+ str (x),'y='+Str (y)3...returnX +y4 ...5>>> Reduce (Add,range (10))6X=0 Y=1#when the 3rd argument does not pass, it can be seen that the value of x is the given value, followed by the superimposed value7X=1 y=28X=3 y=39X=6 y=4Tenx=10 y=5 OneX=15 y=6 AX=21 y=7 -X=28 y=8 -x=36 y=9 the45
* * with a third parameter
1>>> Reduce (Add,range (10), 20)2X=20 y=0#As can be seen from here, reduce is a value from the third parameter and then iterates over the3X=20 Y=14X=21 y=25X=23 y=36X=26 y=47X=30 y=58X=35 y=69X=41 y=7Tenx=48 y=8 Onex=56 y=9 A65 ->>> Reduce (Add,'XYZ') -X=x y=y theX=xy y=Z - 'XYZ' ->>> Reduce (Add,'XYZ', 10) -x=10 y=x + Traceback (most recent): -File"<stdin>", Line 1,inch<module> +File"<stdin>", Line 3,inchAdd Atypeerror:unsupported operand type (s) for+:'int' and 'Str' at>>> ->>> ->>> Reduce (Add,'XYZ','Ten') -x=10 y=x -x=10x y=y -X=10xy y=Z in '10xyz' ->>>
Map
First parameter: Method name
Second parameter: sequence, or multiple sequences (provided the method must support multiple parameters)
The return value is a list
1>>> Map (Add,range (10))#from here it can be seen that the way in which the parameters are read and reduce is different. It's pretty much the same, because the third parameter of reduce is initialized2 Traceback (most recent):3File"<stdin>", Line 1,inch<module>4Typeerror:add () takes exactly 2 arguments (1given)5>>>6>>>7>>> map (Add,range), range (10))8X=0 y=09X=1 Y=1Tenx=2 y=2 OneX=3 y=3 AX=4 y=4 -X=5 y=5 -X=6 y=6 theX=7 y=7 -X=8 y=8 -X=9 y=9 -[0, 2, 4, 6, 8, 10, 12, 14, 16, 18] +>>> ->>> Map (ret,'x') +x=x A['x'] at>>> ->>> ->>> Map (ret,)) -X=1 -x=2 -[1, 2] in>>> ->>> to>>> Map (ret,[1,2]) +X=1 -x=2 the[1, 2] *>>>
If a method in map has only one parameter, only one sequence can be passed in, and if there are multiple, multiple sequences are passed in. It's strange to iterate over the values in each sequence, and I don't know what it's useful to do in the coding process.
In general, I seldom use these methods in the Python coding process, which is estimated to be used during the interview.
Generated by Haroopad
Python's boring summary