這篇文章給大家分享的是實例詳解python高階函數的使用,內容挺不錯的,希望可以幫助到有需要的朋友。
函數參數問題
#三種基本狀況
fun(a,b)
#fun(a,b) b是可重複物件
fun(a,**b) b是具有檢索式迭代對象,在函數體內部解析的時候相當字典
ls = [i for i in range(10)] #print(ls) def fun1(a,*b): for i in b: print(a,i) #fun1(1,*ls) def fun2(name,age,**kw): print("name:",name,"age:",age,"other:",kw) fun2('fanhaitao','26',参数= 'random')
**kw参数把键值对转换成字典的形式
#匿名函數
lambda
沒有return回傳值,回傳值就是表達式的值
函數沒有名字,不用擔心名字衝突
匿名函數也是函數對象,也可以把匿名函數賦值給一個變量,在利用變數來呼叫該函數
fun3 = lambda x:x+1 print(fun3(99)) #100 fun4 = lambda x,y :x*x + y*y print(fun4(3,4)) #25裝飾器@
沒有參數的裝飾器
#定义一个装饰器 def log(func): def wrapper(*args,**kw): print('call %s()' % func.__name__) return func(*args,**kw) return wrapper @log def now(): print('2018-3-29') now()
裝飾器內部邏輯關係(呼叫順序):log() -> return wrapper() -> return func() -> now()
含參數的裝飾器
#定义一个装饰器 def log(text): def decorator(func): def wrapper(*args,**kw): print('%s %s():' % (text,func.__name__)) return func(*args,**kw) return wrapper return decorator @log("可爱的参数") def now(): print('2018-3-29') now()
BiF內建函數
註:多餘的沒有配對到的變數會被捨棄
a = [1,2,3] b = 'abcde' for i in zip(a,b): print(i) for i,j in zip(a,b): print("Index:",i,";Item:",j)
enumerate:返回的是迭代對象,由位置+元素構成for i,j in enumerate('abcde'): print(i,j)
兩個參數,第一個是參數,第二個是可迭代對象,返回的值也是可迭代對象;參數中迭代對像在函數中是True是,值會保留,否者pass
print(list(filter(lambda x:(x*x+x+2)%8 == 0,range(100))))
map
用法和filter類似,差異如下:
參數裡的函數對每個迭代物件操作
#傳回被操作過的物件
#print(list(map(lambda x:x**2,range(5))))
from functools import reduce add = lambda x,y:x+y ls = [i for i in range(101)] print(reduce(add,ls))#######5050###############相關推薦:##############Python之高階函數##### #############Python函數與高階語法集 ###################python函數高階用法#########################################################################
以上是實例詳解python高階函數的使用的詳細內容。更多資訊請關注PHP中文網其他相關文章!

SlicingaPythonlistisdoneusingthesyntaxlist[start:stop:step].Here'showitworks:1)Startistheindexofthefirstelementtoinclude.2)Stopistheindexofthefirstelementtoexclude.3)Stepistheincrementbetweenelements.It'susefulforextractingportionsoflistsandcanuseneg

numpyallowsforvariousoperationsonArrays:1)basicarithmeticlikeaddition,減法,乘法和division; 2)evationAperationssuchasmatrixmultiplication; 3)element-wiseOperations wiseOperationswithOutexpliitloops; 4)

Arresinpython,尤其是Throughnumpyandpandas,weessentialFordataAnalysis,offeringSpeedAndeffied.1)NumpyArseNable efflaysenable efficefliceHandlingAtaSetSetSetSetSetSetSetSetSetSetSetsetSetSetSetSetsopplexoperationslikemovingaverages.2)

列表sandnumpyArraysInpythonHavedIfferentMemoryfootprints:listSaremoreFlexibleButlessMemory-效率,而alenumpyArraySareSareOptimizedFornumericalData.1)listsStorReereReereReereReereFerenceStoObjects,with withOverHeadeBheadaroundAroundaround64byty64-bitsysysysysysysysysyssyssyssyssysssyssys2)

toensurepythonscriptsbehavecorrectlyacrycrosdevelvermations,分期和生產,USETHESTERTATE:1)Environment varriablesForsimplesettings,2)configurationfilesfilesForcomPlexSetups,3)dynamiCofforComplexSetups,dynamiqualloadingForaptaptibality.eachmethodoffersuniquebeneiquebeneqeniquebenefitsandrefitsandrequiresandrequiresandrequiresca

Python列表切片的基本語法是list[start:stop:step]。 1.start是包含的第一個元素索引,2.stop是排除的第一個元素索引,3.step決定元素之間的步長。切片不僅用於提取數據,還可以修改和反轉列表。

ListSoutPerformarRaysin:1)DynamicsizicsizingandFrequentInsertions/刪除,2)儲存的二聚體和3)MemoryFeliceFiceForceforseforsparsedata,butmayhaveslightperformancecostsinclentoperations。

toConvertapythonarraytoalist,usEthelist()constructororageneratorexpression.1)intimpthearraymoduleandcreateanArray.2)USELIST(ARR)或[XFORXINARR] to ConconverTittoalist,請考慮performorefformanceandmemoryfformanceandmemoryfformienceforlargedAtasetset。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

Atom編輯器mac版下載
最受歡迎的的開源編輯器

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!