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 lambda function

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

Share

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

This article mainly introduces how to use the lambda function, the article is very detailed, has a certain reference value, interested friends must read it!

Lambda functions are also called anonymous functions, which are functions that do not have a function name. The lambda expression is named based on the λ calculus in mathematics and corresponds directly to the lambda abstraction in it.

The syntax of the lambda function contains only one statement: lambda [arg1 [, arg2,.argn]]: expression

There are parameters in front of the colon, which can be separated by commas, and the one to the right of the colon is the return value. What the lambda statement builds is actually a function object.

For example, the following code multiplies two numbers:

Def fun (x, y): return x * yprint (fun (2, 3)) # 6

If we use lambda to write, a lambda expression can be done without using def:

Fun = lambda x, y: X * yprint (fun (2,3)) # 6lambda use

Lambda is more concise than normal functions, but it cannot be shared and called anywhere else. So generally, this code does not need to be reused anywhere else, so you can consider using lambda, so you can omit the name of the function.

Python provides a lot of functional programming features, such as filter, reduce, map, sorted and so on all support functions as parameters, lambda functions can be used in functional programming.

# filterlist1 = [3, 12, 9, 25, 17, 36, 14, 17, 27] list2 = filter (lambda x: X% 3 = = 0, list1) print (list (list2)) # [3, 12, 9, 36, 27] # maplist1 = [3, 12, 9, 25, 17, 36, 14, 17, 27] list2 = map (lambda x: X * 10, list1) print (list (list2) # [30,120,90,250,170,360,140,170,270] # reducefrom functools import reducelist1 = [3 12, 9, 25, 17, 36, 14, 17, 27] print (reduce (lambda x, y: X * y, list1)) # 31854967200

Summarizing the use of lambda is simple, but don't blindly use lambda to see if you need to use it again.

The above is all the contents of the article "how to use the lambda function". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow 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