Python tutorial: Python3 anonymous functions and lambda Detailed examples of use

Python tutorial: Python3 anonymous functions and lambda Detailed examples of use

Python tutorial: Python3 anonymous functions and lambda Detailed examples of use

 

Outline

Anonymous function, the name suggests that there is no difference between the maximum function of function, and def defined name that returns the function itself after an anonymous function is created (that is, anonymous functions do not return to the return value), the result is the return value of the expression itself, but after def create is assigned to a variable name, in Python, we create an anonymous function using the keyword lambda, the lambda expression is in the form of an anonymous function:

lambda arg1,arg2,.....argn:expression

The following is a characteristic of some of lambda:

  1. lambda is an expression, not a statement that we can use in any scene as lambda expressions can be used.
  2. Lambda is an expression of the body, and that is defined as a function def, lambda also has the function body, but the body is just a lambda expression, so its use functions subject to greater restrictions.

lambda use

No-argument anonymous function

# Lambda can be passed directly to a variable, like the general function call using the same 
B = the lambda: True
Print (B ())
# is equivalent to
DEF the BF ():
return True
Print (the BF ())

Sample results:

True
True

There are anonymous function parameters

Support for multiple parameters

No default parameter values

two_sum = lambda x, y: x + y
# 等同于:
def two_sum(x, y): return x + y
print(two_sum(1,2))

Sample results:

3

Parameters with default values

sum_with_100 = lambda x, y=100: x + y
# 等同于:
def sum_with_100(x, y=100): return x + y
print(sum_with_100(200))

Sample results:

300

Parameter passing from behind

The previous example we will assign a variable lambda anonymous function, def embodiment functions similarly defined by transmission parameters, we can pass parameters directly behind lambda:

two_sum = (lambda x, y: x + y)(3, 4)
print(two_sum)

Sample results:

7

Nest

The normal function nested lambda, lambda value of the function itself as the return build a simple closure

sum_with_100 = sum(100)
result = sum_with_100(200)
print(result)

Sample results:

300

Some examples of use

1. The two combined in the minimum valued triplet expression evaluation

lower = lambda x,y: x if x<y else y
print(lower(7,100))

Examples of results:

7

2. The key to a sort of dictionary

= D [{ "order":. 3}, { "order":. 1}, { "order": 2}] 
# The order sort key
d.sort (the lambda Key = X: X [ 'order'])
Print (d)

Examples of results

[{'order': 1}, {'order': 2}, {'order': 3}]

About anonymous lambda function today to share with you so much, it should be quite easy to understand! More Python tutorial knowledge will continue to share with you, or you want to learn what the Python chapter and knowledge also can leave a message Oh!

Guess you like

Origin www.cnblogs.com/cherry-tang/p/11009461.html