python中的Lambda表达式/函数
Explanation:
說明:
In python, there is a function named Lambda. Lambda function is an anonymous function - that means the function which does not have any name.
在python中,有一個名為Lambda的函數。 Lambda函數是一個匿名函數-表示該函數沒有任何名稱。
When we declare a function, we use "def" keyword to define a function with a suitable function name. But lambda function does not require that.
當我們聲明一個函數時,我們使用“ def”關鍵字來定義一個具有合適函數名的函數。 但是lambda函數不需要。
Syntax to declare a lambda expression/function:
聲明lambda表達式/函數的語法:
lambda parameterlist : expressionWhere,
哪里,
lambda is a reserved word that defines a lambda expression.
lambda是定義lambda表達式的保留字。
parameterlist is a comma-separated list of parameters as you would find in the function definition (but notice the lack of parentheses).
參數列表是用逗號分隔的參數列表,您可以在函數定義中找到(但請注意缺少括號)。
expression is a single Python expression. The expression cannot be a complete statement.
expression是單個Python表達式。 該表達式不能是完整的語句。
Note:
注意:
This function can take as many arguments as it needs but the expression must be single.
該函數可以根據需要使用任意數量的參數,但表達式必須為單個。
You are free to use the lambda function wherever you want to use.
您可以隨時隨地使用lambda函數。
Example 1: Elaborating about the difference between the Simple function and the Lambda function.
示例1:詳細說明簡單函數和Lambda函數之間的區別。
# simple approach we use to define the area of rectangle: # Python code to illustrate are of rectangle # showing difference between def() and lambda(). def area(l,b): return l*b; g = lambda l,b: l*b print('lambda function:',g(7,4)) #calling the function print('Via Simple function:',area(7,4))Output
輸出量
lambda function: 28 Via Simple function: 28Explanation of the code:
代碼說明:
Here, both of the functions return the same area of a rectangle, But while using the def keyword we need to do all the function of the function and also return it. But same in lambda we just need to give the arguments and the expression which returns the answer accordingly. As it does not include any return statement. We can also put a lambda definition anywhere a function is expected, and we don’t have to assign it to a variable at all. This is the simplicity of lambda functions.
在這里,兩個函數都返回矩形的相同區域,但是在使用def關鍵字時,我們需要完成函數的所有功能并返回它。 但是在lambda中,我們只需要提供參數和相應的表達式即可返回答案。 由于它不包含任何return語句。 我們還可以將lambda定義放在需要函數的任何地方,而我們根本不必將其分配給變量。 這就是lambda函數的簡單性 。
Example2: How we can use lambda function different forms?
例2:我們如何使用lambda函數使用不同的形式?
print('Ways to use and declare lambda functions:') # simply defining lambda function #example - 1 g=lambda x, y: 3*x + y print('Ex-1:',g(10,2))#example - 2 f=lambda x, y: print('Ex-2:',x, y) f(10,2)#example - 3 h=lambda x, y: 10 if x == y else 2 print('Ex-3:',h(5,5))#example - 4 i=lambda x, y: 10 if x == y else 2 print('Ex-4:',i(5,3))Output
輸出量
Ways to use and declare lambda functions: Ex-1: 32 Ex-2: 10 2 Ex-3: 10 Ex-4: 2具有filter(),map(),reduce()的Lambda函數 (Lambda functions with filter() , map() , reduce())
lambda() function can be used with the other functions like filter() , map() etc.
lambda()函數可與其他函數(例如filter() , map()等)一起使用 。
filter(): It takes the list of arguments. This function filters out all the elements in the given list which return True for the function.
filter():獲取參數列表。 該函數過濾掉給定列表中所有返回該函數True的元素。
map(): map() function in python used to map all the elements of the list with its condition by the function or by the lambda function.
map(): python中的map()函數,用于通過函數或lambda函數映射列表中的所有元素及其條件。
Syntax:
句法:
map(function_object, iterable1, iterable2,...)Example 3: Lambda Function using the filter() , map() , reduce()
示例3:使用filter(),map(),reduce()的Lambda函數
#Using the filter() , map() with lambda() function.# Python code to illustrate # filter() with lambda() li = [5, 7, 22, 97, 54, 62, 77, 23, 73, 61] li = list(filter(lambda x: (x%2 == 0) , li)) print('By using filter :',li)# Python code to illustrate # map() with lambda() # to get double of a list. l=[{'name':'includehelp', 'star':10},{'name':'yash', 'star':8},{'name':'sanjeev', 'star':8}] for output1 in (map(lambda x : x['name'],l)):print('maping name:',output1) for output2 in (map(lambda x : x['star']*10,l)):print('maping star:',output2)Output
輸出量
By using filter : [22, 54, 62] maping name: includehelp maping name: yash maping name: sanjeev maping star: 100 maping star: 80 maping star: 80翻譯自: https://www.includehelp.com/python/lambda-expression-function.aspx
總結
以上是生活随笔為你收集整理的python中的Lambda表达式/函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java LineNumberInput
- 下一篇: 对象复制的7种方法,还是Spring的最