PYTHON Learning 0027: an anonymous function function --- ---- 2019-6-20

DEF Calc (X, Y):
return X × Y
can be written by anonymous function:
the lambda X, Y: X × Y
invoke method is:
1, to the anonymous function assignment
FUNC = the lambda X, Y: X × Y
2, then call, as a normal function call:
FUNC = the lambda X, Y: X * Y
FUNC (FUNC (5,6))
output:
30

Note: Anonymous functions do not support such a complicated judgment logic statements, statements can write simple, supports up to three yuan write operation, such as:
FUNC = the lambda X, Y: X * Y IF X <Y the else X / Y
Print (func (8,6))
output: 1.3333333333333333

Knowledge Point: map () Usage:
map () function syntax: the Map (function, Iterable, ...)
function - a function
iterable - one or more sequences
map () will do the mapping function in accordance with the specified sequence provided.
The return value
Python 2.x returns a list.

Python 3.x returns an iterator.

The first parameter argument function to call the function function of each element in the sequence, each time the function returns a new list of function return values.
Examples
The following example shows a map () method is used:

def square (x): # calculate the number of squares
... return X 2
...
Map (Square, [1,2,3,4,5]) # calculates the square of each element of the list
[1, 4, 9, 16 25]
if **
is, the code amount can save anonymous function: **
Map (lambda X: X
2, [1, 2,. 3, 4,. 5]) using lambda # anonymous function
[1, 4, 9 , 16, 25]

Guess you like

Origin blog.51cto.com/13543767/2411349