一文了解 Python 迭代器介绍及其作用
迭代器:初探
Python 學(xué)習(xí)的人都知道,Python 中存在兩種循環(huán)語句:while 和 for。for 循環(huán)可以用于 Python 中的任何序列,包括列表、元組、字符串。
>>> for x in [2013, 14, 15926]: print(x, end=' ') ... 2013 14 15926 >>> >>> for x in (2021, 2022, 2023): print(x, end='->') ... 2021->2022->2023-> >>> for x in 'HelloWorld': print(x, end=' ') ... H e l l o W o r l d實(shí)際上,for 循環(huán)還能使用于?任何可迭代對(duì)象?。可迭代對(duì)象在 Python 中是新穎特別的概念,但實(shí)際上就是序列概念的通用化:如果對(duì)象時(shí)實(shí)際保存的序列,或者可以在迭代工具中(如 for 循環(huán))一次產(chǎn)生一個(gè)結(jié)果的對(duì)象,就看做可迭代的。可以說,Python 中迭代器無處不在。
什么是迭代器?
Python 中的迭代器是一個(gè)對(duì)象,用于迭代列表、元組、字典和集合等可迭代對(duì)象。Python 迭代器對(duì)象必須實(shí)現(xiàn)兩個(gè)特殊的方法:??__iter__()???和??__next__()?? 方法:
-
使用 ??__iter__() ??方法初始化迭代器對(duì)象
-
使用 ??__next__() ?? 方法進(jìn)行迭代。
通過迭代器進(jìn)行迭代
??iter()??? 函數(shù)依次調(diào)用 ??__iter__()?? 方法,返回一個(gè)迭代器。我們使用 ??next() ??? 函數(shù)手動(dòng)遍歷迭代器的所有項(xiàng)。
當(dāng)我們到達(dá)終點(diǎn)并且沒有更多數(shù)據(jù)要返回時(shí),它將引發(fā) ??StopIteration?? 異常。下面是一個(gè)例子:
# define a list my_list = [2013, 14, 15926]# get an iterator using iter() my_iter = iter(my_list)# iterate through it using next()# Output: 2013 print(next(my_iter))# Output: 14 print(next(my_iter))# next(obj) is same as obj.__next__()# Output: 15926 print(my_iter.__next__())# This will raise error, no items left next(my_iter)依次執(zhí)行上面的代碼,輸出如下:
2013 14 15926 Traceback (most recent call last):File "<string>", line 24, in <module>next(my_iter) StopIteration一種更優(yōu)雅的自動(dòng)迭代方式是使用 for 循環(huán)。使用它,我們可以迭代任何可以返回迭代器的對(duì)象,例如列表、字符串、文件等。
>>> for element in my_list: ... print(element) ... 2013 14 15926迭代器 for 循環(huán)的工作
正如我們?cè)谏厦娴氖纠锌吹降?#xff0c;for 循環(huán)能夠自動(dòng)遍歷列表。
實(shí)際上 for 循環(huán)可以迭代任何可迭代對(duì)象。讓我們仔細(xì)看看 for 循環(huán)是如何在 Python 中實(shí)際實(shí)現(xiàn)的。
for element in iterable:# do something with element實(shí)際實(shí)現(xiàn)為:
# create an iterator object from that iterable iter_obj = iter(iterable)# infinite loop while True:try:# get the next itemelement = next(iter_obj)print(element)# do something with elementexcept StopIteration:# if StopIteration is raised, break from loopbreak所以在內(nèi)部,for 循環(huán)通過在可迭代對(duì)象上調(diào)用 ??iter()??? 創(chuàng)建一個(gè)迭代器對(duì)象 ??iter_obj??。具有諷刺意味的是,這個(gè) for 循環(huán)實(shí)際上是一個(gè)無限的 while 循環(huán)。
在循環(huán)內(nèi)部,它調(diào)用 ??next()??? 來獲取下一個(gè)元素并使用該值執(zhí)行 for 循環(huán)的主體。在所有項(xiàng)目耗盡后,??StopIteration?? 被引發(fā),內(nèi)部捕獲并結(jié)束循環(huán)。請(qǐng)注意,任何其他類型的異常都會(huì)通過。
構(gòu)建自定義迭代器
在 Python 中從頭開始構(gòu)建迭代器很容易。我們只需要實(shí)現(xiàn) ??__iter__() ???和 ??__next__() ??方法。
??__iter__() ??方法返回迭代器對(duì)象本身。如果需要,可以執(zhí)行一些初始化。
??__next__() ???方法必須返回序列中的下一項(xiàng)。在到達(dá)終點(diǎn)時(shí)以及在隨后的調(diào)用中,它必須引發(fā) ??StopIteration??。
class PowTwo:"""Class to implement an iteratorof powers of two"""def __init__(self, max=0):self.max = maxdef __iter__(self):self.n = 0return selfdef __next__(self):if self.n <= self.max:result = 2 ** self.nself.n += 1return resultelse:raise StopIteration# create an object numbers = PowTwo(3)# create an iterable from the object i = iter(numbers)# Using next to get to the next iterator element print(next(i)) print(next(i)) print(next(i)) print(next(i)) print(next(i))輸出結(jié)果:
1 2 4 8 Traceback (most recent call last):File "/Users/yuzhou_1su/go/src/iterdemo.py", line 32, in <module>print(next(i)) StopIteration我們還可以使用 for 循環(huán)來迭代我們的迭代器類。
>>> for i in PowTwo(5): ... print(i) ... 1 2 4 8 16 32Python 無限迭代器
迭代器對(duì)象中的項(xiàng)目不必耗盡。可以有無限的迭代器(永遠(yuǎn)不會(huì)結(jié)束)。在處理此類迭代器時(shí),我們必須小心。
這是一個(gè)演示無限迭代器的簡單示例。
內(nèi)置函數(shù) ??iter()?? 可以使用兩個(gè)參數(shù)調(diào)用,其中第一個(gè)參數(shù)必須是可調(diào)用對(duì)象(函數(shù)),第二個(gè)參數(shù)是哨兵。迭代器調(diào)用這個(gè)函數(shù),直到返回的值等于哨兵。
>>> int() 0>>> inf = iter(int,1) >>> next(inf) 0 >>> next(inf) 0我們可以看到 ??int()??? 函數(shù)總是返回 0。因此將它作為 ??iter(int,1)??? 傳遞將返回一個(gè)迭代器,該迭代器調(diào)用 ??int()?? 直到返回值等于 1。這永遠(yuǎn)不會(huì)發(fā)生,我們得到一個(gè)無限迭代器。
我們還可以構(gòu)建自己的無限迭代器。理論上,以下迭代器將返回所有奇數(shù):
class InfIter:"""Infinite iterator to return allodd numbers"""def __iter__(self):self.num = 1return selfdef __next__(self):num = self.numself.num += 2return num >>> a = iter(InfIter()) >>> next(a) 1 >>> next(a) 3 >>> next(a) 5 >>> next(a) 7在對(duì)這些類型的無限迭代器進(jìn)行迭代時(shí),請(qǐng)小心包含終止條件。如上所示,我們可以得到所有奇數(shù),而無需將整個(gè)數(shù)字系統(tǒng)存儲(chǔ)在內(nèi)存中。理論上,我們可以在有限的內(nèi)存中擁有無限的項(xiàng)目。
Python 迭代器的好處
使用迭代器的好處是可以節(jié)省資源。
-
代碼減少。
-
代碼冗余得到極大解決。
-
降低代碼復(fù)雜度。
-
它為編碼帶來了更多的穩(wěn)定性。
總結(jié)
Python 的迭代器提供穩(wěn)定和靈活的代碼。迭代器和可迭代對(duì)象的區(qū)別:
-
?Iterable? 是一個(gè)可以迭代的對(duì)象。它在傳遞給 ??iter()?? 方法時(shí)生成一個(gè)迭代器。
-
?Iterator? 是一個(gè)對(duì)象,用于使用 ??__next__()??? 方法對(duì)可迭代對(duì)象進(jìn)行迭代。迭代器有 ??__next__() ??方法,它返回對(duì)象的下一項(xiàng)。
請(qǐng)注意,每個(gè)迭代器也是一個(gè)可迭代的,但不是每個(gè)可迭代的都是一個(gè)迭代器。
例如,列表是可迭代的,但列表不是迭代器。可以使用函數(shù) ??iter() ??從可迭代對(duì)象創(chuàng)建迭代器。
為了實(shí)現(xiàn)這一點(diǎn),對(duì)象的類需要一個(gè)方法 ??__iter__???,它返回一個(gè)迭代器,或者一個(gè)具有從 0 開始的順序索引的 ??__getitem__??? 方法。但其本質(zhì)也是實(shí)現(xiàn)了 ??__iter__?? 方法。
參考資料:
-
??https://www.geeksforgeeks.org/iterators-in-python/??
-
??Python Iterators??
?
總結(jié)
以上是生活随笔為你收集整理的一文了解 Python 迭代器介绍及其作用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2015考研英语作文万能模板,只需要写一
- 下一篇: Ubuntu命令之alias - 设置命