日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python中的Lambda表达式/函数

發(fā)布時間:2025/3/11 python 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python中的Lambda表达式/函数 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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的函數(shù)。 Lambda函數(shù)是一個匿名函數(shù)-表示該函數(shù)沒有任何名稱。

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.

當(dāng)我們聲明一個函數(shù)時,我們使用“ def”關(guān)鍵字來定義一個具有合適函數(shù)名的函數(shù)。 但是lambda函數(shù)不需要。

Syntax to declare a lambda expression/function:

聲明lambda表達(dá)式/函數(shù)的語法:

lambda parameterlist : expression

Where,

哪里,

  • lambda is a reserved word that defines a lambda expression.

    lambda是定義lambda表達(dá)式的保留字。

  • parameterlist is a comma-separated list of parameters as you would find in the function definition (but notice the lack of parentheses).

    參數(shù)列表是用逗號分隔的參數(shù)列表,您可以在函數(shù)定義中找到(但請注意缺少括號)。

  • expression is a single Python expression. The expression cannot be a complete statement.

    expression是單個Python表達(dá)式。 該表達(dá)式不能是完整的語句。

Note:

注意:

  • This function can take as many arguments as it needs but the expression must be single.

    該函數(shù)可以根據(jù)需要使用任意數(shù)量的參數(shù),但表達(dá)式必須為單個。

  • You are free to use the lambda function wherever you want to use.

    您可以隨時隨地使用lambda函數(shù)。

Example 1: Elaborating about the difference between the Simple function and the Lambda function.

示例1:詳細(xì)說明簡單函數(shù)和Lambda函數(shù)之間的區(qū)別。

# 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: 28

Explanation 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.

在這里,兩個函數(shù)都返回矩形的相同區(qū)域,但是在使用def關(guān)鍵字時,我們需要完成函數(shù)的所有功能并返回它。 但是在lambda中,我們只需要提供參數(shù)和相應(yīng)的表達(dá)式即可返回答案。 由于它不包含任何return語句。 我們還可以將lambda定義放在需要函數(shù)的任何地方,而我們根本不必將其分配給變量。 這就是lambda函數(shù)的簡單性 。

Example2: How we can use lambda function different forms?

例2:我們?nèi)绾问褂胠ambda函數(shù)使用不同的形式?

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函數(shù) (Lambda functions with filter() , map() , reduce())

lambda() function can be used with the other functions like filter() , map() etc.

lambda()函數(shù)可與其他函數(shù)(例如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():獲取參數(shù)列表。 該函數(shù)過濾掉給定列表中所有返回該函數(shù)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()函數(shù),用于通過函數(shù)或lambda函數(shù)映射列表中的所有元素及其條件。

Syntax:

句法:

map(function_object, iterable1, iterable2,...)

Example 3: Lambda Function using the filter() , map() , reduce()

示例3:使用filter(),map(),reduce()的Lambda函數(shù)

#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

總結(jié)

以上是生活随笔為你收集整理的python中的Lambda表达式/函数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。