此頁面的目的是示範 2 個迭代協定的動態:
- 可迭代
- 迭代器
1. 但首先(加入令人困惑的相似單字),讓我們解決迭代問題
- 迭代 - 當然 - 是從來源中逐項獲取項目並依次對每個項目執行某些操作
- 在Python中,這常用在
- a) for/while 迴圈和
- b) 理解
- 預設 - 這些結構迭代整個結構
- 然而,有時可能需要更細粒度的控制 - 就像在生成器中
- 為此,有 2 個重要的概念/協議,Python 的大部分都是在它們之上構建的:
- a) 可迭代物件
- b) 迭代器物件
- 兩者都反映在標準Python協定中
- 這不是額外的東西:實際上,for/while 循環和推導式直接構建在迭代協議的這些較低級別元素上
2. ITER() 方法從可迭代物件建立迭代器
- 可迭代物件(物件的集合或流)是任何可以傳遞到內建 iter() 函數的物件
- 一旦傳遞了內建的 iter() 函數,它就會傳回一個傳遞類型的 迭代器物件,也就是建立一個字串迭代器
>>> example_iterator = iter('abc') >>> example_iterator <str_iterator object at> </str_iterator>
- 請注意,迭代器是一個隱式序列對象,提供順序(不是隨機!)對底層順序資料集的存取
- 例如範圍物件本身不是迭代器
- 迭代器不允許存取底層序列的任意元素
- 它們僅提供對系列下一個元素的存取
- 它們提供順序存取
<!-- THIS IS NOT AN ITERATOR --> >>> r = range(10)[5] >>> r 5
3. NEXT() 函數從迭代器傳回下一個值
- 內建的 next() 需要一個 迭代器 物件 - 它傳回集合迭代中的下一個值
- 迭代器由 2 個組件組成:
- 擷取集合的下一個元素的機制
- 表示系列結束的機制
在具有內建物件系統的程式語言中,這種抽象通常對應於可以由類別實現的特定介面
- next() 允許依要求依序考慮每個項目 - 而不是從開始到結束的整個系列
- 有2個訊息迭代器介麵包括
- 下一個 → 查詢下一個元素
- iter → 回傳迭代器
- 限制:迭代器只能迭代一次
4. 課堂範例 - 從可迭代到迭代器再到停止迭代異常
- Python 會隨意引發 StopIteration 類型的 異常
>>> example_iterator = iter('abc') >>> example_iterator <str_iterator object at> </str_iterator>
5. 實際範例 - 對多個命令列輸入進行單元測試
- 定義/取得可迭代對象,例如列表 ["20.01", "y"]
- 將可迭代物件傳遞給 iter() → 建立迭代器物件
- 將迭代器物件傳遞到 next() 中,以便每次在程式碼中呼叫輸入函數時產生列表的下一個值
<!-- THIS IS NOT AN ITERATOR --> >>> r = range(10)[5] >>> r 5
- 第一次遇到input()時,傳遞的是「20.01」值,
- 第二次是“y”
- 第三次就例外了
6. 連結
- https://mypy.readthedocs.io/en/stable/protocols.html#iteration-protocols
- 5.2 隱式序列 - Python 中的 SICP
以上是解釋 Python 中的 Iterable 與 Iterator的詳細內容。更多資訊請關注PHP中文網其他相關文章!

ForhandlinglargedatasetsinPython,useNumPyarraysforbetterperformance.1)NumPyarraysarememory-efficientandfasterfornumericaloperations.2)Avoidunnecessarytypeconversions.3)Leveragevectorizationforreducedtimecomplexity.4)Managememoryusagewithefficientdata

Inpython,ListSusedynamicMemoryAllocationWithOver-Asalose,而alenumpyArraySallaySallocateFixedMemory.1)listssallocatemoremoremoremorythanneededinentientary上,respizeTized.2)numpyarsallaysallaysallocateAllocateAllocateAlcocateExactMemoryForements,OfferingPrediCtableSageButlessemageButlesseflextlessibility。

Inpython,YouCansspecthedatatAtatatPeyFelemereModeRernSpant.1)Usenpynernrump.1)Usenpynyp.dloatp.dloatp.ploatm64,formor professisconsiscontrolatatypes。

NumPyisessentialfornumericalcomputinginPythonduetoitsspeed,memoryefficiency,andcomprehensivemathematicalfunctions.1)It'sfastbecauseitperformsoperationsinC.2)NumPyarraysaremorememory-efficientthanPythonlists.3)Itoffersawiderangeofmathematicaloperation

Contiguousmemoryallocationiscrucialforarraysbecauseitallowsforefficientandfastelementaccess.1)Itenablesconstanttimeaccess,O(1),duetodirectaddresscalculation.2)Itimprovescacheefficiencybyallowingmultipleelementfetchespercacheline.3)Itsimplifiesmemorym

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)


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

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

熱門文章

熱工具

Dreamweaver CS6
視覺化網頁開發工具

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

SublimeText3漢化版
中文版,非常好用

WebStorm Mac版
好用的JavaScript開發工具

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器