Simple analysis of lambda function in Python (with examples)

不言
Release: 2018-10-10 15:40:40
forward
8469 people have browsed it

This article brings you a simple analysis of the lambda function in Python (with examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. The lambda function is also called an anonymous function, that is, the function does not have a specific name. Let’s look at the simplest example first:

def f(x):
return x**2
print f(4)
Copy after login

If you use lambda in Python, it is written like this

g = lambda x : x**2
print g(4)
Copy after login

2. Compared with ordinary functions, lambda just omits the function name, and at the same time like this Anonymous functions cannot be shared and called elsewhere.

In fact, you are right. Lambda does not play an earth-shattering role in a dynamic language like Python, because there are many other methods that can replace lambda.

1. When using Python to write some execution scripts, using lambda can save the process of defining functions and make the code more streamlined.

2. For some abstract functions that will not be reused elsewhere, sometimes it is difficult to name the function. There is no need to consider naming issues when using lambda.

3. Use lambda to make the code easier to understand at certain times.

lambda basics

In the lambda statement, there are parameters before the colon. There can be multiple parameters, separated by commas, and the return value on the right side of the colon. The lambda statement actually constructs a function object. Witness it:

>>> foo = [2, 18, 9, 22, 17, 24, 8, 12, 27]
>>> print filter(lambda x: x % 3 == 0, foo)
[18, 9, 24, 12, 27]
>>> print map(lambda x: x * 2 + 10, foo)
[14, 46, 28, 54, 44, 58, 26, 34, 64]
>>> print reduce(lambda x, y: x + y, foo)
139
Copy after login

In terms of object traversal processing, Python’s for..in..if syntax is actually very powerful and is easier to read than lambda .

defaultdict is a dictionary type. You can set the default value for defaultdict and set the default value through lambda.

Here are a few examples:

from collections import *                                                      
x = defaultdict(lambda:0) //默认值是0
print x[0]
y =defaultdict(lambda:defaultdict(lambda:0))//默认值是一个字典,字典的默认值是0
print y[0]
z = defaultdict(lambda:[0,0,0])//默认值是一个列表,[0,0,0].
print z[0]
Copy after login

Output results:

0
defaultdict(<function <lambda> at0x7f097797af50>, {})
[0, 0, 0]
Copy after login

The above is the detailed content of Simple analysis of lambda function in Python (with examples). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template