Python高級(jí)特性與幾種函數(shù)的講解
切片
從list或tuple中取部分元素。
list = [1, 2, 3, 4] list[0 : 3] # [1, 2, 3] list[-2 : -1] # -1表示最后一個(gè),[3, 4] list[1 :: 2] # index = 1開始,每?jī)蓚€(gè)取一個(gè)[2, 4] list[:] # 復(fù)制list,[1, 2, 3, 4] # 針對(duì)tuple,切片同樣適用
iterable、iterator
可迭代,迭代器,集合類型數(shù)據(jù)可迭代但不是迭代器,可通過iter()轉(zhuǎn)變?yōu)榈鳌?/p>
可迭代對(duì)象可使用for-in語句遍歷,判斷x是否可迭代:isinstance(x, Iterable)。
列表生產(chǎn)式
高效創(chuàng)建列表,見代碼示例:
# range轉(zhuǎn)list list(range(1, 5)) # [1, 2, 3, 4] [x * x for x in range(1, 5)] # [1, 4, 9, 16] [x * x for x in range(1, 5) if x % 2 == 0] # [4, 16] [m + n for m in 'ABC' for n in 'XYZ'] # ['AX', 'AY', 'AZ', 'BX', 'BY', 'BZ', 'CX', 'CY', 'CZ'] [s.lower() for s in ['Hello', 'World', 'IBM', 'Apple']] # like map
generator
isinstance(generator, Iterable) = True,可使用for-in語句,或者使用next方法。
g = (x * x for x in range(10)) next(g) # 0 next(g) # 1 next(g) # 4 for item in g: print(item) # 9 16 ... 81
generator函數(shù)
generator函數(shù)本質(zhì)是一個(gè)有狀態(tài)的函數(shù),遇到y(tǒng)ield語句時(shí)會(huì)暫時(shí)返回。
# 有yield語句,表明時(shí)generator函數(shù)
def gen_fn():
init = 0
while init < 10:
yield init
init += 1
return 'done'
call = gen_fn() # 獲得可迭代對(duì)象call
next(call) # 0
next(call) # 1
# 每次調(diào)用generator函數(shù),得到的是一個(gè)新的generator
# for-in無法獲得generator的返回值'done'
for item in gen_fn():
print(item) # 0 1 ... 9
高階函數(shù)
參數(shù)是函數(shù)的函數(shù)即是高階函數(shù),可對(duì)比數(shù)學(xué)概念:g(x) = f(x) + 1,g(x)即高階函數(shù)。
- map
# map(func, *iterables, ...) i = map(lambda x : x * x, [1, 2, 3]) # 返回Iterator list(i) # [1, 4, 9]
- reduce
from functools import reduce reduce(lambda previous, x : previous + x, [1, 2, 3, 4]) # 10
- filter
i = filter(lambda x : x % 2 == True, [1, 2, 3, 4]) list(i) # [1, 3]
- sorted 默認(rèn)升序,通過key參數(shù)決定排序規(guī)則。
sorted([1,3,2], key = lambda x : -x) # [3, 2, 1]
返回函數(shù)做回函數(shù)返回值
閉包概念:包含環(huán)境成分(自由變量)和控制成分的實(shí)體(lambda表達(dá)式,函數(shù))。
def lazy_sum(*args):
ax = 0
def sum():
nonlocal ax
for n in args:
ax = ax + n
return ax
return sum
fn = lazy_sum(1, 2, 3) # ax + sum構(gòu)成了閉包
fn() # 6
fn() # 12
匿名函數(shù)
即lambda表達(dá)式。
裝飾器
函數(shù)包函數(shù)的語法糖?
def log(fn):
def call(*args, **kw):
print('call %s():' % fn.__name__)
return fn(*args, **kw)
return call
# @log的作用等同now = log(now)
@log
def now():
print('2018-03-18')
now() # call now(): 2018-03-18
偏函數(shù)
把一個(gè)函數(shù)的某些參數(shù)給固定住,返回一個(gè)新的函數(shù)。類似柯里化,但更強(qiáng)大?
from functools import partial
binary_int = partial(int, base = 2)
binary_int('1000000') # 64
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
相關(guān)文章
對(duì)python pandas中 inplace 參數(shù)的理解
這篇文章主要介紹了對(duì)python pandas中 inplace 參數(shù)的理解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-06-06
淺談tensorflow語義分割api的使用(deeplab訓(xùn)練cityscapes)
這篇文章主要介紹了淺談tensorflow語義分割api的使用(deeplab訓(xùn)練cityscapes),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05
python網(wǎng)絡(luò)爬蟲精解之正則表達(dá)式的使用說明
正則表達(dá)式是對(duì)字符串操作的一種邏輯公式,就是用事先定義好的一些特定字符、及這些特定字符的組合,組成一個(gè)“規(guī)則字符串”,這個(gè)“規(guī)則字符串”用來表達(dá)對(duì)字符串的一種過濾邏輯2021-09-09
django框架之cookie/session的使用示例(小結(jié))
這篇文章主要介紹了django框架之cookie/session的使用示例(小結(jié)),詳細(xì)的介紹了cookie和session技術(shù)的接口獲取等問題,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10
Python選擇網(wǎng)卡發(fā)包及接收數(shù)據(jù)包
今天小編就為大家分享一篇關(guān)于Python選擇網(wǎng)卡發(fā)包及接收數(shù)據(jù)包,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-04-04
用Python生成器實(shí)現(xiàn)微線程編程的教程
這篇文章主要介紹了用Python生成器實(shí)現(xiàn)微線程編程的教程,本文來自于IBM官方開發(fā)者技術(shù)文檔,需要的朋友可以參考下2015-04-04
終端命令查看TensorFlow版本號(hào)及路徑的方法
今天小編就為大家分享一篇終端命令查看TensorFlow版本號(hào)及路徑的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-06-06

