本篇內(nèi)容主要講解“Python iter()的用法有哪些”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學(xué)習(xí)“Python iter()的用法有哪些”吧!
一、上代碼、學(xué)用法
我們都比較熟悉 iter(obj),會返現(xiàn)一個迭代器,如果 obj 不是可迭代對象,則會報錯。但其實如果仔細看官方文檔,會發(fā)現(xiàn) iter() 方法其實是接受兩個參數(shù)的,文檔說明如下
iter(object[, sentinel])
sentinel 英文翻譯為 哨兵。
sentinel 參數(shù)是可選的,當(dāng)它存在時,object 不再傳入一個可迭代對象,而是一個可調(diào)用對象,通俗點說就是可以通過()調(diào)用的對象,而 sentinel 的作用就和它的翻譯一樣,是一個“哨兵”,當(dāng)時可調(diào)用對象返回值為這個“哨兵”時,循環(huán)結(jié)束,且不會輸出這個“哨兵”。
可能有點難懂,用一個簡單需求來說明,需求說明如下:
心里想一個[1, 10]范圍的數(shù),然后代碼開始隨機,當(dāng)隨機到想的數(shù)時停止,看每次代碼需要隨機幾次。
實現(xiàn)分析:看起來應(yīng)該很簡單,random,然后加一個if判斷即可,但是用 iter() 來實現(xiàn)更簡單。實現(xiàn)代碼如下:
from random import randint def guess(): return randint(0, 10) num = 1 # 這里先寫死心里想的數(shù)為5 for i in iter(guess, 5): print("第%s次猜測,猜測數(shù)字為: %s" % (num, i)) num += 1 # 當(dāng) guess 返回的是 5 時,會拋出異常 StopIteration,但 for 循環(huán)會處理異常,即會結(jié)束循環(huán)
二、還是看看文檔吧
關(guān)于這兩個參數(shù),文檔里也說的很詳細,分段解釋如下:
The first argument is interpreted very differently depending on the presence of the second argument.
翻譯:第一個參數(shù)根據(jù)第二個參數(shù)有不同的含義
Without a second argument, object must be a collection object which supports the iteration protocol (the _iter_() method), or it must support the sequence protocol (the _getitem_() method with integer arguments starting at 0). If it does not support either of those protocols, TypeError is raised.
翻譯:如果沒有第二個參數(shù),object(即第一個參數(shù))是一個支持迭代器協(xié)議(實現(xiàn)_iter_()方法的)的集合對象,或者是支持序列協(xié)議(實現(xiàn)_getitem_()方法)且是從0開始索引。如果它不支持其中任何一個,則拋出 TypeError 異常
簡單來說就是,如果沒有第二個參數(shù),就是我們比較熟悉的用法。代碼示例如下:
In [5]: iter("123") Out[5]: <str_iterator at 0x105c9b9e8> In [6]: iter([1, 2, 3]) Out[6]: <list_iterator at 0x105f9f8d0> In [7]: iter(123) --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-7-c76acad08c3c> in <module>() ----> 1 iter(123) TypeError: 'int' object is not iterable
再來看看有第二個參數(shù)的情況
If the second argument, sentinel, is given, then object must be a callable object. The iterator created in this case will call object with no arguments for each call to its _next_() method; if the value returned is equal to sentinel, StopIteration will be raised, otherwise the value will be returned.
翻譯:如果給定了第二個參數(shù) sentinel,object 則必須是一個可調(diào)用對象,這個可調(diào)用對象沒有任何參數(shù),當(dāng)可調(diào)用對象的返回值等于 sentinel 的值時,拋出 StopIteration 的異常,否則返回當(dāng)前值。(這里如果不好理解可調(diào)用對象,可以理解為函數(shù),這樣更容易想明白)
對于這個用法的適用場景,文檔中也給出了說明:
One useful application of the second form of iter() is to build a block-reader. For example, reading fixed-width blocks from a binary database file until the end of file is reached:
翻譯:對于第二個參數(shù),一個有用的場景是創(chuàng)建一個 blokc-reader,即根據(jù)條件中斷讀取。比如:從二進制數(shù)據(jù)庫文件讀取固定寬度的塊,直到到達文件的末尾,代碼示例如下:
from functools import partial with open('mydata.db', 'rb') as f: for block in iter(partial(f.read, 64), b''): process_block(block)
到此,相信大家對“Python iter()的用法有哪些”有了更深的了解,不妨來實際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
當(dāng)前題目:Pythoniter()的用法有哪些-創(chuàng)新互聯(lián)
標(biāo)題URL:http://jinyejixie.com/article40/dppjeo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)、App開發(fā)、軟件開發(fā)、虛擬主機、服務(wù)器托管、App設(shè)計
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)