[Tips] Python lambda expression (anonymous function) and its application in the built-in functions

lambda expressions, also known as anonymous functions, ie functions no specific name, used to represent internal function contains only one line expression.

Using lambda write code more simple and compact, but lambda function support functionality is very limited, currently PEP8 no longer recommend the use of lambda, but def recommended to define a function , even so, as a Python programmer, knowledge and understanding the anonymous function is still necessary.

  • Just a lambda expression, function body is much simpler than def.
  • Lambda expression is a body, instead of a code block. We can only package a limited logic into the lambda expression.
  • lambda function has its own namespace, and can not be accessed outside of its own argument list or the global namespace parameters.
  • Although lambda function looks can only write a single line, but not equivalent to C or C ++ inline functions, which aims not take up the stack memory when calling small functions to increase operating efficiency.

lambda syntax:lambda argument(s):expression

lambda functions can have many arguments, but only one expression. lambda operator can not have any statement, it returns a function object, this function can be assigned to any variable objects.

lambda functions simple example:

lambda x, y: x * y     # 函数输入是 x 和 y,输出是它们的积 x*y
lambda:None     # 函数没有输入参数,输出是 None
>>> a = lambda x: x+1
>>> a(1)
2

# 用 def 函数表示如下:

>>> def a(x):
		return x+1
>>> a(1)
2

The main difference between the lambda functions and def:

  • lambda can be passed immediately (no variable), automatically returns the result;
  • lambda contain only one line of code inside;
  • lambda is to write a simple function and design, but def to handle more tasks;
  • lambda can define an anonymous function, but def defined function must have a name.

lambda functions advantages:

  • For one-way function, the process eliminates the need to use lambda expressions defined functions, make the code more concise;
  • For the function does not require multiple re-use, the use of lambda expressions can be released immediately after the run, improve the performance of program execution.

lambda functions are often used in some built-in functions of:

map() Function: traversal sequence, each element of a sequence operation, when lambda functions common to specify the operating conditions for each list element.

>>> list1 = [1, 2, 3, 4]
>>> list2 = [5, 6, 7 ,8]
>>> list3 = map(lambda a, b: a + b, list1, list2)     # 将两个列表中对应的元素相加
>>> print(list3)
<map object at 0x000001EA57C07780>
>>> print(list(list3))
[6, 8, 10, 12]

filter() Function: sequence elements for filter operation, when the conditions for lambda functions specified filter list element.

>>> list1 = [1, 2, 3, 4]
>>> list2 = filter(lambda a: a > 2, list1)     # 筛选出列表中大于 2 的元素
>>> print(list2)
<filter object at 0x000001EA57C07898>
>>> print(list(list2))
[3, 4]

reduce() Function: For all of the sequence elements accumulation operation, when the conditions for accumulating lambda functions specified list of two adjacent elements.

>>> from functools import reduce
>>> list1 = [1, 2, 3, 4]
>>> list2 = reduce(lambda a, b: a + b, list1)     # 两两相邻元素进行相加操作
>>> print(list2)
10
>>> list2 = reduce(lambda a, b: a * b, list1)     # 两两相邻元素进行相乘操作
>>> print(list2)
24

sorted() Function: elements for a sequence sorting operation, when lambda functions specified condition for all of the elements in the list to be sorted.

>>> list1 = [1, 2, 3, 4, 5, 6, 7, 8]
>>> list2 = sorted(list1, key = lambda a: abs(4-a))     # 将列表元素按照与 4 的距离从小到大进行排序
>>> print(list2)
[4, 3, 5, 2, 6, 1, 7, 8]

Additional information: "Do not write a lambda expression in Python, do not recommend you use it."

Published 149 original articles · won praise 518 · Views 460,000 +

Guess you like

Origin blog.csdn.net/qq_36759224/article/details/104399083