Python高級特性與幾種函數(shù)的講解
切片
從list或tuple中取部分元素。
list = [1, 2, 3, 4] list[0 : 3] # [1, 2, 3] list[-2 : -1] # -1表示最后一個,[3, 4] list[1 :: 2] # index = 1開始,每兩個取一個[2, 4] list[:] # 復(fù)制list,[1, 2, 3, 4] # 針對tuple,切片同樣適用
iterable、iterator
可迭代,迭代器,集合類型數(shù)據(jù)可迭代但不是迭代器,可通過iter()轉(zhuǎn)變?yōu)榈鳌?/p>
可迭代對象可使用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ì)是一個有狀態(tài)的函數(shù),遇到y(tǒng)ield語句時會暫時返回。
# 有yield語句,表明時generator函數(shù) def gen_fn(): init = 0 while init < 10: yield init init += 1 return 'done' call = gen_fn() # 獲得可迭代對象call next(call) # 0 next(call) # 1 # 每次調(diào)用generator函數(shù),得到的是一個新的generator # for-in無法獲得generator的返回值'done' for item in gen_fn(): print(item) # 0 1 ... 9
高階函數(shù)
參數(shù)是函數(shù)的函數(shù)即是高階函數(shù),可對比數(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ù)
把一個函數(shù)的某些參數(shù)給固定住,返回一個新的函數(shù)。類似柯里化,但更強(qiáng)大?
from functools import partial binary_int = partial(int, base = 2) binary_int('1000000') # 64
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
相關(guān)文章
python將字典內(nèi)容存入mysql實(shí)例代碼
這篇文章主要介紹了python將字典內(nèi)容存入mysql實(shí)例代碼,具有一定借鑒價值,需要的朋友可以參考下2018-01-01mac下pycharm設(shè)置python版本的圖文教程
今天小編就為大家分享一篇mac下pycharm設(shè)置python版本的圖文教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06Python中的collections集合與typing數(shù)據(jù)類型模塊
這篇文章介紹了Python中的collections集合與typing數(shù)據(jù)類型模塊,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05Python cookbook(數(shù)據(jù)結(jié)構(gòu)與算法)同時對數(shù)據(jù)做轉(zhuǎn)換和換算處理操作示例
這篇文章主要介紹了Python cookbook(數(shù)據(jù)結(jié)構(gòu)與算法)同時對數(shù)據(jù)做轉(zhuǎn)換和換算處理操作,涉及Python使用生成器表達(dá)式進(jìn)行數(shù)據(jù)處理的相關(guān)操作技巧,需要的朋友可以參考下2018-03-03pyhton學(xué)習(xí)與數(shù)據(jù)挖掘self原理及應(yīng)用分析
這篇文章主要為大家介紹了深入分析pyhton中的self原理及應(yīng)用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2021-11-11Django自定義插件實(shí)現(xiàn)網(wǎng)站登錄驗(yàn)證碼功能
這篇文章主要為大家詳細(xì)介紹了Django自定義插件實(shí)現(xiàn)網(wǎng)站登錄驗(yàn)證碼功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-04-04