簡(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ī)矩,官方文檔走一波:
針對(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ù)禾唁。
那么從官方文檔的介紹來(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ò)濾判斷。
對(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)俏橘!