Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python anonymous function lambda

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, I would like to share with you the relevant knowledge about how to use the Python anonymous function lambda. The content is detailed and the logic is clear. I believe most people still know too much about this, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

1. Preface

In Python, when it comes to functions, it's easy to think of declaring a function with the def keyword:

Def Hello (): # function body

Then we can add a list of parameters enclosed in parentheses. There may be many lines of code inside the function, with as many statements and expressions as possible.

In addition to the def statement defining the function, there is another form of expression that generates the function object: the lambda expression, which creates a function that can be called at any time.

Sometimes there is only one statement to declare a function, such as:

Def square (x): return Xero2

No lambda function returns the function itself instead of assigning it to a variable name. So it is also called anonymous function name). The general form of lambda is the keyword lambda followed by one or more parameters, followed by a colon, followed by an expression:

Lambda argument1, argument2,... ArgumentN: expression using argument

The function object returned by the lambda expression works exactly like the function object created and assigned by def. A lambda function can have as many arguments as possible, but the function body must be an expression.

two。 How to use lambda

Lambda can contain only one expression, which is usually used as a definition of an inline function, or to postpone the execution of some code.

Let's take a look at the square example above:

Def square (x): return x*xa_list = [1,2,3,4,5] aa_list = list (map (square, a_list)) print (aa_list) # [1,4,9,16,25]

The above code is a good place to use the lambda function, using fewer lines of code, and avoiding creating named functions that are used only once (which must then be stored in memory). You can write a lambda function that squares its parameters: lambda x: X. square 2 and use it with the map function to square all the elements in the list:

A_list = [1,2,3,4,5,6] aa_list = list (map (lambda x: xylene 2, a_list)) print (aa_list) # [2,4,6,8,10,12]

If you need a small function in a short period of time, you can use the lambda function-- for example, as an argument to a higher-order function such as map or filter, and we can use lambda to filter even numbers:

A_list = [1, 2, 3, 4, 5, 6] aa_list = list (filter (lambda x: X% 2 / 4) a_list) print (aa_list) # [2,4,6]

The lambda function is defined where it is used, so there are no named functions in memory. If you only use one function in one place, it makes sense to use the lambda function to avoid confusion.

You can also return the lambda function from the function. If you need to create multiple functions that multiply numbers, such as doubling or tripling, lambda can help. Instead of creating multiple functions, you can create a function multiplyBy, as shown below, and then call the function multiple times with different parameters to create double, triple, and so on:

Def muliplyBy (n): return lambda x: Xeron double = multiplyBy (2) triple = muliplyBy (3) times10 = multiplyBy (10)

The lambda function takes the value n from the parent function, so the value of n is 2 in double, 3 in threefold, and 10 in times10. Now calling these functions with arguments will multiply by that number.

Double (6) > 12triple (5) > 15times10 (12) > 120

If you are not using the lambda function here, you need to define a different function in multiplyBy

As follows:

Def muliplyBy (x): def temp (n): return Xenn return temp

Use the lambda function to use half the lines and make them more readable.

These are all the contents of the article "how to use Python anonymous function lambda". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report