五月综合激情婷婷六月,日韩欧美国产一区不卡,他扒开我内裤强吻我下面视频 ,无套内射无矿码免费看黄,天天躁,日日躁,狠狠躁

新聞動態(tài)

Python編程如何在遞歸函數(shù)中使用迭代器

發(fā)布日期:2021-12-30 08:12 | 文章來源:源碼中國

首先,想要實現(xiàn)的功能是遞歸遍歷文件夾,遇到滿足條件的文件時,用yield返回該文件的位置。

如果不用遞歸器,可以這樣實現(xiàn):

path_list = []
def get_one_cage(root: str, cook_folder_name: str):
 for item in os.listdir(root).copy():
  item_path = os.path.join(root, item)
  if item == cook_folder_name:
path_list.append(item_path)
return
  elif os.path.isdir(item_path):
get_one_cage(item_path, cook_folder_name)

即,深度優(yōu)先遍歷,滿足要求時,將item_path補充到列表里,之后返回上一層。
這里有一個問題,需要有一個列表,把所有滿足條件的地址全存起來,占內(nèi)存。

使用迭代器可以用一個,遍歷出來一個,省內(nèi)存

替換為迭代器,最先想到的是,把 return 換成 yield,使用for循環(huán)調(diào)用迭代器函數(shù)

def get_one_cage(root: str, cook_folder_name: str):
 for item in os.listdir(root).copy():
  item_path = os.path.join(root, item)
  if item == cook_folder_name:
yield item_path
  elif os.path.isdir(item_path):
get_one_cage(item_path, cook_folder_name)

但是這樣的程序跑到內(nèi)嵌函數(shù)時,進不去,我百思不得其解

現(xiàn)在看,應(yīng)該是因為迭代器函數(shù)不是一個函數(shù),不是一個命令語句,它只是一個對象。

簡單說就是,python程序一般遵循:動詞+名詞的結(jié)構(gòu),或者動詞,比如:

a = 1

這句話實際上是把1賦值給了a,是有動詞的。
迭代器只是一個名詞,必須用for語句調(diào)用或者next()方法調(diào)用才會執(zhí)行,或者是print,yield,return等等,反正得加個動詞,不能孤零零一個名詞。
而且上述代碼還有一個漏洞。在第一段代碼中,我們用一個全局變量存放遍歷結(jié)果。在第二段代碼里,我們本意是把結(jié)果yield到for循環(huán)調(diào)用的地方,但事實是,程序已經(jīng)套了好幾層了,每次yiled只能返回一層。如下圖所示:

綜上兩點作出如下修改:

def get_one_cage(root: str, cook_folder_name: str):
 for item in os.listdir(root).copy():
  item_path = os.path.join(root, item)
  if item == cook_folder_name:
yield item_path
  elif os.path.isdir(item_path):
yield get_one_cage(item_path, cook_folder_name)

程序執(zhí)行結(jié)果如下:

顯然是返回了一個迭代器,不是一個str,其邏輯如下圖所示:

就好比,本意是:
小明把沙袋傳給小紅,小紅傳給小蘭
但現(xiàn)在是:
小明把沙袋傳給了小紅,小紅被傳了出去
修改如下:

def get_one_cage(root: str, cook_folder_name: str):
 for item in os.listdir(root).copy():
  item_path = os.path.join(root, item)
  if item == cook_folder_name:
yield item_path
  elif os.path.isdir(item_path):
yield next(get_one_cage(item_path, cook_folder_name))

邏輯如下:

還有一種情況是學(xué)長源碼里的:使用for調(diào)用迭代器:

def get_one_cage(root: str, cook_folder_name: str):
 for item in os.listdir(root).copy():
  item_path = os.path.join(root, item)
  if item == cook_folder_name:
yield item_path
  elif os.path.isdir(item_path):
 for i in get_one_cage(item_path, cook_folder_name):
  yield i

這使用于多個文件的返回,源碼里還配合isfile使用,這里是簡化版,所以顯得冗余。
兩種方式均可以正常使用。

昨天這篇文章寫完后,遇到了bug,簡單說就是,如果一個文件夾系統(tǒng)沒有我們想要的文件,遞歸到最深一層文件夾時,會報錯

1
可以理解為:老板讓員工找一樣?xùn)|西,員工外包給編外人員。如果編外人員找到了想要的東西,一路傳遞回去,可以正常交差。如果沒找到,編外人員就會一直找,不停歇,找遍了所有能找到的地方(遍歷完整個文件夾)也沒能找到,就會報錯StopIteration。
因此,問題核心是,沒有一個返回機制。修改辦法是在遍歷最后加一個空返回

def get_one_cage(root: str):
 for index, item in enumerate(os.listdir(root)):
  item_path = os.path.join(root, item)
  if item == 'cooked_xyz':
yield item_path
  elif os.path.isdir(item_path):
yield next(get_one_cage(item_path))
  elif index == len(os.listdir(root).copy()) - 1:
yield

或者是利用try… except語句處理異常:

def get_one_cage(root: str):
 try:
  for item in os.listdir(root):
item_path = os.path.join(root, item)
if item == 'cooked_xyz':
 yield item_path
elif os.path.isdir(item_path):
 yield next(get_one_cage(item_path))
 except:
  yield

會有如上報錯,正常。
最后的yield換成return也是可以的,但最好還是用yield,兩個混起來用怪怪的。

個人推薦第二種方法
注:copy()可以不用要

以上就是Python編程如何在遞歸中使用迭代器的詳細內(nèi)容,更多關(guān)于Python編程遞歸中使用迭代器的資料請關(guān)注本站其它相關(guān)文章!

以上就是Python編程如何在遞歸函數(shù)中使用迭代器的詳細內(nèi)容,更多關(guān)于Python遞歸函數(shù)中使用迭代器的資料請關(guān)注本站其它相關(guān)文章!

版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。

相關(guān)文章

實時開通

自選配置、實時開通

免備案

全球線路精選!

全天候客戶服務(wù)

7x24全年不間斷在線

專屬顧問服務(wù)

1對1客戶咨詢顧問

在線
客服

在線客服:7*24小時在線

客服
熱線

400-630-3752
7*24小時客服服務(wù)熱線

關(guān)注
微信

關(guān)注官方微信
頂部