Python小世界:匿名函數(shù)后添、高階函數(shù)笨枯、推導(dǎo)式

簡(jiǎn)述

閑話不多說(shuō),本篇博文吕朵,主要針對(duì)Python的

匿名函數(shù)lambda

高階函數(shù)map reduce filter

推導(dǎo)式list set dict

三個(gè)方面來(lái)匯總猎醇。

如果有想要學(xué)習(xí)Python或者正在學(xué)習(xí)Python中的小伙伴,需要學(xué)習(xí)資料的話努溃,可以到我的微信公眾號(hào):Python學(xué)習(xí)知識(shí)圈硫嘶,后臺(tái)回復(fù):“01”,即可拿Python學(xué)習(xí)資料

匿名函數(shù)

當(dāng)我們?cè)趥魅牒瘮?shù)時(shí)梧税,有些時(shí)候沦疾,不需要顯式地定義函數(shù),那么此時(shí)匿名函數(shù)就灰常方便了第队。

示例: lambda a, b: a + b 實(shí)際上就是下面代碼的簡(jiǎn)寫(xiě)

<pre class="hljs python" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 0.75em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">def func(a, b):
return a + b
復(fù)制代碼</pre>

對(duì)于匿名函數(shù)而言哮塞,不用寫(xiě) return返回值就是該表達(dá)式的結(jié)果 凳谦。 因?yàn)闆](méi)有函數(shù)名字忆畅,不必?fù)?dān)心函數(shù)名的沖突,此外尸执,匿名函數(shù)也是一個(gè)函數(shù)對(duì)象家凯,可以把匿名函數(shù)賦值給一個(gè)變量,再利用變量來(lái)調(diào)用該函數(shù):

<pre class="prettyprint hljs ruby" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">>>> f = lambda x: x * x

f
<function <lambda> at 0x10453d7d0>
f(6)
36
復(fù)制代碼</pre>

那么在一些簡(jiǎn)單的情況下如失,盡情的使用匿名函數(shù)吧绊诲。

高階函數(shù)

何為高階函數(shù)?

能接受函數(shù)做參數(shù)的函數(shù)

因Python中一切皆對(duì)象褪贵,變量名可以指向函數(shù)掂之,而函數(shù)的參數(shù)可以接收變量,那么一個(gè)函數(shù)就可以接收另外一個(gè)函數(shù)作為參數(shù)脆丁。這就是傳說(shuō)中的 高階函數(shù) 世舰。

map()

老規(guī)矩,官方文檔走一波:

Python官方文檔--map()

針對(duì)map(function, iterable, ...)函數(shù)槽卫,可 結(jié)合lambda 使用跟压,示例如下:

<pre class="prettyprint hljs ruby" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">>>> list(map(lambda x:x*x, [1,2,3,4,5]))

[1, 4, 9, 16, 25]
復(fù)制代碼</pre>

注:Python3中,需要使用list()將map函數(shù)返回值轉(zhuǎn)化為列表晒夹,若無(wú)list()裆馒,則結(jié)果為:

<pre class="prettyprint hljs ruby" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">>>> map(lambda x:x*x, [1,2,3,4,5])

<map at 0x20b225167f0>
復(fù)制代碼</pre>

此外姊氓,map()函數(shù)不改變?cè)械?list,而是返回一個(gè)新的 list喷好。

reduce()

為便于掌握翔横,對(duì)比,在總結(jié)完map()函數(shù)后梗搅,我們來(lái)看下reduce()函數(shù)禾唁。

Python官方文檔--reduce()

那么從官方文檔的介紹來(lái)看:

reduce()函數(shù)接收的參數(shù)和 map()類(lèi)似,一個(gè)函數(shù) f无切,一個(gè)list荡短,但行為和 map()不同,reduce()傳入的函數(shù) f 必須接收兩個(gè)參數(shù)哆键,reduce()對(duì)list的每個(gè)元素反復(fù)調(diào)用函數(shù)f掘托,并返回最終結(jié)果值。

示例如下:

<pre class="prettyprint hljs python" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">from functools import reduce

reduce(lambda x, y: x + y, [1, 2, 3, 4, 5])
復(fù)制代碼</pre>

對(duì)結(jié)果演示即: ((((1+2)+3)+4)+5) = 15 注:

reduce()函數(shù)可接收第三個(gè)參數(shù)籍嘹,作為函數(shù)的起始值

filter()

filter()函數(shù)顧名思義闪盔,進(jìn)行過(guò)濾判斷。

Python官方文檔--filter()

對(duì)于filter()函數(shù)來(lái)說(shuō)辱士,其 接收一個(gè)函數(shù) f 和一個(gè)list泪掀,這個(gè)函數(shù) f 的作用是對(duì)每個(gè)元素進(jìn)行判斷,返回 True或 False颂碘。

示例:過(guò)濾出1~100中平方根是整數(shù)的數(shù):

<pre class="prettyprint hljs python" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">import math

def func(x):
r = int(math.sqrt(x)) # math.sqrt()計(jì)算平方根
if r * r == x:
return x

list(filter(func, range(1, 101)))
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
復(fù)制代碼</pre>

那么從該示例中异赫,我們能夠得出結(jié)論: filter()根據(jù)判斷結(jié)果自動(dòng)過(guò)濾掉不符合條件的元素,返回由符合條件元素組成的新list头岔。

推導(dǎo)式

推導(dǎo)式在日常工作中是比較好的裝逼利器塔拳,對(duì)于列表,字典切油,集合的操作蝙斜,很多時(shí)候一行代碼即可解決名惩,如若沒(méi)有澎胡,那說(shuō)明內(nèi)力還不夠深厚,嘎嘎嘎娩鹉。攻谁。。弯予。 對(duì)于推導(dǎo)式而言戚宦,我們就從

列表推導(dǎo)式

字典推導(dǎo)式

集合推導(dǎo)式

來(lái)總結(jié),當(dāng)然也就這三種锈嫩。受楼。垦搬。

列表推導(dǎo)式

示例:

<pre class="prettyprint hljs python" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">from random import randint

[randint(1, 10) for _ in range(20)]
[8, 2, 7, 9, 7, 3, 10, 10, 2, 10, 5, 9, 4, 7, 9, 2, 10, 6, 10, 7]
復(fù)制代碼</pre>

字典推導(dǎo)式

示例:

<pre class="prettyprint hljs python" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">>>> {x: x * x for x in range(10) if x % 3 == 0}

{0: 0, 3: 9, 6: 36, 9: 81}
復(fù)制代碼</pre>

集合推導(dǎo)式

鑒于集合具有去重效果,那么我們創(chuàng)建示例艳汽,來(lái)和列表推導(dǎo)式對(duì)比:

<pre class="prettyprint hljs python" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">from random import randint

{randint(1, 10) for _ in range(20)}
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
復(fù)制代碼</pre>

很神奇有木有猴贰,目前寫(xiě)的只是最基本的推導(dǎo)式寫(xiě)法,在實(shí)際的工作中河狐,可以添加各種判斷米绕,隨意靈活運(yùn)用。

總結(jié)

本篇博客側(cè)重于實(shí)際工作中代碼的簡(jiǎn)化馋艺,重構(gòu)栅干。若能結(jié)合實(shí)際工作需求,靈活運(yùn)用捐祠,則能大大簡(jiǎn)化代碼碱鳞,也方便他人閱讀,久而久之踱蛀,自己的水平也逐漸提高劫笙。 起止一個(gè)爽字了得!P歉凇填大! 江湖有緣,下期再見(jiàn)俏橘!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末允华,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子寥掐,更是在濱河造成了極大的恐慌靴寂,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,682評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件召耘,死亡現(xiàn)場(chǎng)離奇詭異百炬,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)污它,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,277評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén)剖踊,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人衫贬,你說(shuō)我怎么就攤上這事德澈。” “怎么了固惯?”我有些...
    開(kāi)封第一講書(shū)人閱讀 165,083評(píng)論 0 355
  • 文/不壞的土叔 我叫張陵梆造,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我葬毫,道長(zhǎng)镇辉,這世上最難降的妖魔是什么屡穗? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,763評(píng)論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮忽肛,結(jié)果婚禮上鸡捐,老公的妹妹穿的比我還像新娘。我一直安慰自己麻裁,他們只是感情好箍镜,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,785評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著煎源,像睡著了一般色迂。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上手销,一...
    開(kāi)封第一講書(shū)人閱讀 51,624評(píng)論 1 305
  • 那天歇僧,我揣著相機(jī)與錄音,去河邊找鬼锋拖。 笑死诈悍,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的兽埃。 我是一名探鬼主播侥钳,決...
    沈念sama閱讀 40,358評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼柄错!你這毒婦竟也來(lái)了舷夺?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 39,261評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤售貌,失蹤者是張志新(化名)和其女友劉穎给猾,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體颂跨,經(jīng)...
    沈念sama閱讀 45,722評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡敢伸,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,900評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了恒削。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片池颈。...
    茶點(diǎn)故事閱讀 40,030評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖蔓同,靈堂內(nèi)的尸體忽然破棺而出饶辙,到底是詐尸還是另有隱情蹲诀,我是刑警寧澤斑粱,帶...
    沈念sama閱讀 35,737評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站脯爪,受9級(jí)特大地震影響则北,放射性物質(zhì)發(fā)生泄漏矿微。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,360評(píng)論 3 330
  • 文/蒙蒙 一尚揣、第九天 我趴在偏房一處隱蔽的房頂上張望涌矢。 院中可真熱鬧,春花似錦快骗、人聲如沸娜庇。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,941評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)名秀。三九已至,卻和暖如春藕溅,著一層夾襖步出監(jiān)牢的瞬間匕得,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,057評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工巾表, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留汁掠,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,237評(píng)論 3 371
  • 正文 我出身青樓集币,卻偏偏與公主長(zhǎng)得像考阱,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子鞠苟,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,976評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容