anonymous function lambda
Lambda Argument1,argument2,... argumentn:expression using arguments
1, Lambda is an expression, not a statement.
Because of this, lambda can appear where the DEF is not allowed in the Python syntax---for example, in a list constant or in the parameters of a function call.
2. The body of a lambda is a single expression, not a block of code.
Lambda is designed for writing simple functions, and DEF is used to handle larger tasks.
For example:
Lambda notation:
F=lambda X,y,z:x+y+zprint (f (2,3,4)) printed results: 9
Function notation:
def f (x, Y, z):
Return x+y+z
Print (f (2,3,4))
Printed results: 9
Lambda notation:
Foo=lambda a= ' lll ', b= ' que ', c= ' Rade ': A+b+cprint (foo (' Mas ')) Printing results: Masquerade
Function notation:
def foo (a= ' lll ', b= ' que ', c= ' Rade '):
Return A+b+c
Print (foo (' Mas '))
Printing results:
Masquerade
L=[lambda X:x**2,lambda X:x**3,lambda x:x**4]for f in L: Print (f (2)) Results: 4816
Print (L[1] (3)) printing results:
27
Nested lambda
Type one:
def foo (x): return (lambda y:x+y) Bar=foo print (bar (2)) printed results: 101
Type two:
Foo=lambda x: (Lambda y:x+y)
Bar=foo (99)
Print (bar (2))
Formula Three:
Print ((lambda x: (Lambda Y:x+y) (99)) (2))
Printing results:
101
Filter function
The filter function performs a filtering operation on the specified sequence.
Definition: Filter (function or None, sequence)->iterator
The filter function calls the function function for each element in the sequence parameter sequence, and the result that is returned contains the element that invokes the result to true.
Returns an iterative object that requires a list call to display all results.
def f (x): if x>20: return True else: return Falsel1=[2,8,15,22,36]print (Filter (F,L1)) L2=filter ( F,L1) Print (l2.__next__ ()) #22print (l2.__next__ ()) #36print (l2.__next__ ()) #StopIteration # Or we can use a for loop to traverse the filter l2:for I in L2: print (i) Results: 2236
Print (List (filter (lambda x:x>0,range ( -5,5))) #[1, 2, 3, 4]
Print (List (filter (none,[1,2,3,4,5))) #[1, 2, 3, 4, 5]
Map (func,iter1,iter2) functions, iterator
As you can see by definition, the first parameter of this function is a function, the remaining argument is one or more sequences, and the return value is an iterator.
Function can be understood as a one-to-one or many-to-a function, the role of map is to call the function function as each element in the parameter sequence, and return the iterator containing the return value of each function.
Returns an iterative object that needs to be called List to display all results.
def f (x, y): return (x, y) L1 = [0, 1, 2, 3, 4, 5, 6]l2 = [' Sun ', ' M ', ' t ', ' W ', ' t ', ' f ', ' S ']l3=map (F,L1,L2) # Print (L3) #<map object at 0x0000000002301470>print (list (L3)) #[(0, ' Sun '), (1, ' M '), (2, ' t '), (3, ' W '), (4, ' t '), (5, ' F '), ( 6, ' S ')]def F2 (x): return X**2print (list (map (F2,L1))) #[0, 1, 4, 9,, 36]def f3 (x, y): return X*2,y*2pri NT (List (map (F3,L1,L2))) #[(0, ' Sunsun '), (2, ' MM '), (4, ' TT '), (6, ' WW '), (8, ' TT '), (Ten, ' FF '), (n, ' SS ')]
Print (list (map (lambda x:x + 2, [1, 2, 3]))) #[3, 4, 5]print (list (map (pow,[1,2,3],[2,3,4))) #[1, 8, Bayi] #pow (x, y) is to calculate the Y power of x (square)
Reduce function
In Python 3, the reduce () function has been removed from the global namespace and is now placed in the Fucntools module, with the words first introduced: Functools.reduce (function,sequence,initial)
The reduce function accumulates elements in the parameter sequence.
Defined:
The function parameter is a two-parameter, and reduce sequentially takes an element from iterable and invokes function again with the result of the last call to function.
The first call to function, if supplied with the initial parameter, calls function with the first element in iterable and initial as a parameter, otherwise the function is invoked with the first two elements in the iterable.
Equivalent to:
def reduce (function, iterable, initializer=none): it = iter (iterable) if initializer is None: value = Next (IT)
else: value = initializer for element in it: value = function (value, Element) return value
Example:
Import Functoolsprint (Functools.reduce (lambda x,y:x+y,[1,2,3,4)) #10print (functools.reduce (Lambda x,y:x+y,[ 1,2,3,4],10)) #20print (Functools.reduce (lambda x,y:x*y,[1,2,3,4)) #24 ############# #如果没有initial参数, So Calculate: (((((1+2) +3) +4) If there are initial parameters, this is calculated as: (((((((((10+1) +2) +3) +4)
Note: Function functions cannot be none,function must be functions with 2 parameters.
Zip function
Definition: Zip ([seql, ...]) Accepts a series of iterative objects as parameters, packages the corresponding elements in the object into tuple (tuples), and then returns a list of these tuples (lists). If the length of the passed parameter is not equal, the length of the returned list is the same as the object with the shortest length in the parameter.
Print (list (Zip ([1,2,3],[11,22,33])) #两个列表长度一致
#[(1, 11), (2, 22), (3, 33)]
Print (list (Zip ([1,2,3],[11,22,33,44,55])) #两个列表长度不一致, whichever is shorter
#[(1, 11), (2, 22), (3, 33)]
Python common built-in function collation (LAMBDA,REDUCE,ZIP,FILTER,MAP)