Python lambda 函數(shù)
首先,這個(gè)語(yǔ)法跟C++的語(yǔ)法幾乎一樣;
通常稱 lambda 函數(shù)為匿名函數(shù),也稱為 丟棄函數(shù),因?yàn)閼?yīng)一下子就不要了,不會(huì)長(zhǎng)期凝結(jié)下來(lái)形成SDK API;本人覺(jué)得它有點(diǎn)類似 inline 函數(shù),或者叫做 小小函數(shù),一行寫罷;
一, 先看一眼示例
先運(yùn)行要給簡(jiǎn)單的例子,讓問(wèn)題具象一些:
例一: x+x+x
#######################
(base) hipper@hipper-G21:~$ python
Python 3.11.3 (main, Apr 19 2023, 23:54:32) [GCC 11.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>>
>>> triple = lambda x: x + x + x
>>> triple(3)
9
>>> print(triple(2))
6
>>>
#######################文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-670850.html
例二:三維空間歐氏距離
#######################
>>> import math
>>> eucli = lambda x, y, z: math.sqrt(x**2 + y**2 + z**2)
>>> eucli(3,4,0)
5.0
>>>
#######################
其中,這里的triple 和 eucli 是lambda 函數(shù)對(duì)象的指針;
二,lambda函數(shù)出現(xiàn)的場(chǎng)景
那么,lambda函數(shù)用在什么場(chǎng)景呢?
1,在 def 定義的函數(shù)內(nèi)部
#######################
import math
def add_x_y_z(x, y, z):
add = lambda a, b: a+b
sum = add(x, y)
sum = add(sum, z)
return sum
print( add_x_y_z(3, 4, 5))
#######################math 沒(méi)用到
2,lambda 結(jié)合 filter
filter函數(shù),顧名思義是對(duì)list中的每個(gè)元素做過(guò)濾,并返回一個(gè)新的list;
從數(shù)學(xué)考試得分list中,找出優(yōu)秀的分?jǐn)?shù):
#######################
(base) hipper@hipper-G21:~/ex/ex_python/lambda_ex$ ipython
Python 3.11.3 (main, Apr 19 2023, 23:54:32) [GCC 11.2.0]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.12.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: score_list=[77, 65, 47, 83, 77, 97, 89, 51, 92]
In [2]: outstanding_list=list(filter(lambda score: (score>80), score_list))
In [3]: outstanding_list
Out[3]: [83, 97, 89, 92]
In [4]:
#######################
3, lambda 結(jié)合 map
map函數(shù),會(huì)把list中的元素一一作為參數(shù),返回值一一構(gòu)成新的列表;
#######################
(base) hipper@hipper-G21:~$ ipython
Python 3.11.3 (main, Apr 19 2023, 23:54:32) [GCC 11.2.0]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.12.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: num_list=[1,2,3,4,5,6,7]
In [2]: is_even_list=list( map( (lambda num:(num%2==0)) , num_list ) )
In [3]: is_even_list
Out[3]: [False, True, False, True, False, True, False]
In [4]:
#######################
4, reduce 與 lambda結(jié)合
reduce函數(shù)在包functools 中,按照某個(gè)運(yùn)算符一一累算 list中的所有元素
#######################
(base) hipper@hipper-G21:~$ ipython
Python 3.11.3 (main, Apr 19 2023, 23:54:32) [GCC 11.2.0]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.12.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: from functools import reduce
In [2]: num_list=[1,2,3,4,5]
In [3]: sigma=reduce(lambda a1, a2: a1+a2, num_list)
In [4]: sigma
Out[4]: 15
In [5]: order=reduce(lambda a1, a2: a1*a2, num_list)
In [6]:
#######################文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-670850.html
到了這里,關(guān)于Python3 lambda 函數(shù)入門示例 Python lambda 函數(shù)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!