本文實例講述了Python iter()函數用法。分享給大家供大家參考,具體如下:
python中的迭代器用起來非常靈巧,不僅可以迭代序列,也可以迭代表現出序列行為的對象,例如字典的鍵、一個文件的行,等等。
迭代器就是有一個next()方法的對象,而不是通過索引來計數。當使用一個循環機制需要下一個項時,調用迭代器的next()方法,迭代完后引發一個StopIteration異常。
但是迭代器只能向后移動、不能回到開始、再次迭代只能創建另一個新的迭代對象。
反序迭代工具:reversed()將返回一個反序訪問的迭代器。python中提供的迭代模塊:itertools模塊
先看幾個例子:
>>> l=[2,3,4]>>> iterl=iter(l)>>> iterl.next()2>>> iterl.next()3>>> iterl.next()4>>> iterl.next()Traceback (most recent call last): File "<stdin>", line 1, in <module>StopIteration
>>> d={'one':1,'two':2,'three':3}>>> d{'three': 3, 'two': 2, 'one': 1}>>> iterd=iter(d) #字典的迭代器會遍歷字典的鍵(key)>>> iterd.next()'three'>>> iterd.next()'two'>>> iterd.next()'one'>>> iterd.next()Traceback (most recent call last): File "<stdin>", line 1, in <module>StopIteration下面查看iter()函數的幫助信息:
>>> help(iter)Help on built-in function iter in module __builtin__:iter(...) iter(collection) -> iterator iter(callable, sentinel) -> iterator Get an iterator from an object. In the first form, the argument must supply its own iterator, or be a sequence. In the second form, the callable is called until it returns the sentinel.
iter()函數有兩種用法,一種是傳一個參數,一種是傳兩個參數。結果都是返回一個iterator對象。
所謂的iterator對象,就是有個next()方法的對象。next方法的慣例或約定(convention)是,每執行一次就返回下一個值(因此它要自己記錄狀態,通常是在iterator對象上記錄),直到沒有值的時候raiseStopIteration。
傳1個參數:參數collection應是一個容器,支持迭代協議(即定義有__iter__()函數),或者支持序列訪問協議(即定義有__getitem__()函數),否則會返回TypeError異常。
傳2個參數:當第二個參數sentinel出現時,參數callable應是一個可調用對象(實例),即定義了__call__()方法,當枚舉到的值等于哨兵時,就會拋出異常StopIteration。
>>> s='abc' #s支持序列訪問協議,它有__getitem__()方法>>> help(str.__getitem__)Help on wrapper_descriptor:__getitem__(...) x.__getitem__(y) <==> x[y]>>> s.__getitem__(1)'b'>>> s[1]'b'>>> iters=iter(s) #iters是一個iterator對象,它有next()和__iter__()方法>>> iters1=iters.__iter__()>>> iters2=iter(iters)>>> iters<iterator object at 0x030612D0>>>> iters1<iterator object at 0x030612D0>>>> iters2<iterator object at 0x030612D0>iters iters1 iters2 是同一個迭代器!!>>> iters.next()'a'>>> iters.next()'b'>>> iters.next()'c'>>> iters.next()Traceback (most recent call last): File "<stdin>", line 1, in <module>StopIteration
>>> class test: # test 類支持迭代協議,因為它定義有__iter__()函數... def __iter__(self):... print '__iter__ is called!'... self.result=[1,2,3]... return iter(self.result)...>>> t=test() # t支持迭代協議>>> for i in t: #當執行for i in t 時,實際上是調用了t.__iter__(),也就是__iter__(t),返回一個iterator對象... print i,...__iter__ is called!1 2 3>>> for i in t.__iter__(): print i,__iter__ is called!!1 2 3>>> for i in test.__iter__(t): print i,__iter__ is called!!1 2 3>>> l=[1,2,3]>>> for i in l:... print i,...1 2 3
#上述for循環實際上是這樣工作的(for循環會自動調用迭代器的next()方法),如下:>>> iterl=iter(l)>>> while True:... try:... i=iterl.next()... except StopIteration:... break... print i,...1 2 3
>>> f=open(r'C:/Users/Administrator/Desktop/test.txt','w')>>> f.writelines(['love python/n','hello python/n','love python/n'])>>> f.close()>>> f=open(r'C:/Users/Administrator/Desktop/test.txt','r')>>> for line in f: # 文件對象生成的迭代器會自動調用readline()方法,這樣循環遍歷就可以訪問文本文件的所有行... print line[:-1]...love pythonhello pythonlove python
上述for循環部分功能與以下代碼一致:
>>> while True:... line=f.readline()... if line!='':... print line[:-1]... else:... break...love pythonhello pythonlove python
>>> f=open(r'C:/Users/91135/Desktop/test.txt','r')>>> f.readlines()['love python/n', 'hello python/n', '/n', 'love python/n']>>> f.seek(0)>>> f.next()'love python/n'>>> f.next()'hello python/n'>>> f.next()'/n'>>> f.next()'love python/n'>>> f.next()Traceback (most recent call last): File "<pyshell#140>", line 1, in <module> f.next()StopIteration>>> f.seek(0)>>> it1=iter(f)>>> it2=f.__iter__()
f iter1 iter2 三者是同一個對象!!!
>>> f<open file 'C://Users//91135//Desktop//test.txt', mode 'r' at 0x030E9A70>>>> it1<open file 'C://Users//91135//Desktop//test.txt', mode 'r' at 0x030E9A70>>>> it2<open file 'C://Users//91135//Desktop//test.txt', mode 'r' at 0x030E9A70>>>> f.next()'love python/n'>>> it1.next()'hello python/n'>>> next(it2)'/n'>>> next(f)'love python/n'>>> next(f)Traceback (most recent call last): File "<pyshell#247>", line 1, in <module> next(f)StopIteration>>> it1.next()Traceback (most recent call last): File "<pyshell#248>", line 1, in <module> it1.next()StopIteration>>> it2.next()Traceback (most recent call last): File "<pyshell#249>", line 1, in <module> it2.next()StopIteration
iter(callable, sentinel) -> iterator
如果是傳遞兩個參數給 iter() , 第一個參數必須是callable ,它會重復地調用第一個參數,
直到迭代器的下個值等于sentinel:即在之后的迭代之中,迭代出來sentinel就立馬停止。
關于Python中,啥是可調用的,可以參考:python callable()函數
>>> class IT(object): def __init__(self): self.l=[1,2,3,4,5] self.i=iter(self.l) def __call__(self): #定義了__call__方法的類的實例是可調用的 item=next(self.i) print "__call__ is called,which would return",item return item def __iter__(self): #支持迭代協議(即定義有__iter__()函數) print "__iter__ is called!!" return iter(self.l)>>> it=IT() #it是可調用的>>> it1=iter(it,3) #it必須是callable的,否則無法返回callable_iterator>>> callable(it)True>>> it1<callable-iterator object at 0x0306DD90>>>> for i in it1:print i__call__ is called,which would return 11__call__ is called,which would return 22__call__ is called,which would return 3
可以看到傳入兩個參數得到的it1的類型是一個callable_iterator,它每次在調用的時候,都會調用__call__函數,并且最后輸出3就停止了。
>>> it2=iter(it)__iter__ is called!!>>> it2<listiterator object at 0x030A1FD0>>>> for i in it2:print i,1 2 3 4 5
與it1相比,it2就簡單的多,it把自己類中一個容器的迭代器返回就可以了。
上面的例子只是為了介紹iter()函數傳兩個參數的功能而寫,如果真正想寫一個iterator的類,還需要定義next函數,這個函數每次返回一個值就可以實現迭代了。
>>> class Next(): def __init__(self,data=825): self.data=data def __iter__(self): return self def next(self): print "next is called!!" if self.data>828: raise StopIteration else: self.data+=1 return self.data>>> for i in Next():print inext is called!!826next is called!!827next is called!!828next is called!!829next is called!!>>> for i in Next(826):print inext is called!!827next is called!!828next is called!!829next is called!!>>>
唯一需要注意下的就是next中必須控制iterator的結束條件,不然就死循環了。
>>> it=Next()>>> it.__iter__()<__main__.Next instance at 0x02E75F80>>>> Next.__iter__(it)<__main__.Next instance at 0x02E75F80>>>> iter(it)<__main__.Next instance at 0x02E75F80>>>> it<__main__.Next instance at 0x02E75F80>>>> it=Next()>>> it.next()next is called!!826>>> next(it)next is called!!827>>> Next.next(it)next is called!!828>>> next(it)next is called!!829>>> it.next()next is called!!Traceback (most recent call last): File "<pyshell#68>", line 1, in <module> it.next() File "<pyshell#1>", line 9, in next raise StopIterationStopIteration
希望本文所述對大家Python程序設計有所幫助。
新聞熱點
疑難解答