1. 手動(dòng)遍歷迭代器
你想遍歷一個(gè)可迭代對(duì)象中的所有元素晦嵌,但是卻不想使用for循環(huán)同辣。
請(qǐng)使用 next()
函數(shù)并在代碼中捕獲 StopIteration
異常。我們也可提供默認(rèn)值用于標(biāo)記結(jié)尾惭载。
>>> items = [1, 2, 3]
>>> it = iter(items) # Invokes items.__iter__()
>>>
>>> next(it) # Invokes it.__next__()
1
>>> next(it)
2
>>> next(it)
3
>>> next(it)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
>>>
>>> next(it, None)
何為可迭代對(duì)象旱函,需要這個(gè)對(duì)象中實(shí)現(xiàn)了 __iter__()
以及 __next__()
兩個(gè)函數(shù)。
2. 代理迭代
class Node:
def __init__(self, value):
self._value = value
self._children=[]
def __repr__(self):
return 'Node({!r})'.format(self._value)
def add_child(self, child):
self._children.append(child)
def __iter__(self):
return iter(self._children)
if __name__ == '__main__':
root=Node(0)
child1=Node(1)
child2=Node(2)
root.add_child(child1)
root.add_child(child2)
print(root)
for child in root:
print(child)
Python
的迭代器協(xié)議需要 __iter__()
方法返回一個(gè)實(shí)現(xiàn)了 __next__()
方法的迭代器對(duì)象棕兼。 如果你只是迭代遍歷其他容器的內(nèi)容陡舅,你無(wú)須擔(dān)心底層是怎樣實(shí)現(xiàn)的抵乓。你所要做的只是傳遞迭代請(qǐng)求既可伴挚。
這里的 iter()
函數(shù)的使用簡(jiǎn)化了代碼, iter(s)
只是簡(jiǎn)單的通過(guò)調(diào)用 s.__iter__()
方法來(lái)返回對(duì)應(yīng)的迭代器對(duì)象灾炭, 就跟 len(s)
會(huì)調(diào)用 s.__len__()
原理是一樣的茎芋。
3. 使用生成器創(chuàng)建新的迭代模式
如果我們想用函數(shù)實(shí)現(xiàn)生成器函數(shù),那么函數(shù)中需要有一個(gè) yield
語(yǔ)句即可將其轉(zhuǎn)換為一個(gè)生成器蜈出。 跟普通函數(shù)不同的是田弥,生成器只能用于迭代操作,且只能使用一次铡原。
>>> def countdown(n):
... print('Starting to count from', n)
... while n > 0:
... yield n
... n -= 1
... print('Done!')
...
>>>
>>> c=countdown(3)
>>> c
<generator object countdown at 0x101aa93b8>
>>>
>>> next(c)
Starting to count from 3
3
>>> next(c)
2
>>> next(c)
1
>>> next(c)
Done!
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
生成器只能使用一次
>>> n=(n*n for n in range(0, 10))
>>>
>>>
>>> sum(n)
285
>>> sum(n)
0
4. 實(shí)現(xiàn)迭代器協(xié)議
當(dāng)想實(shí)現(xiàn)一個(gè)具有迭代功能的自定義對(duì)象時(shí)偷厦,最好的辦法就是使用生成器函數(shù)商叹。
class Node:
def __init__(self, value):
self._value = value
self._children=[]
def __repr__(self):
return 'Node({!r})'.format(self._value)
def add_child(self, child):
self._children.append(child)
def __iter__(self):
return iter(self._children)
def depth_first(self):
yield self
for c in self:
yield from c.depth_first()
Python
的迭代協(xié)議要求一個(gè) __iter__()
方法返回一個(gè)特殊的迭代器對(duì)象, 這個(gè)迭代器對(duì)象實(shí)現(xiàn)了 __next__()
方法并通過(guò) StopIteration
異常標(biāo)識(shí)迭代的完成只泼。 但是剖笙,實(shí)現(xiàn)這些通常會(huì)比較繁瑣。
我們實(shí)現(xiàn)這個(gè)深度優(yōu)先的遞歸遍歷樹形節(jié)點(diǎn)的生成器请唱,最直接的方式就是使用 yield
弥咪、yield from
將函數(shù)變成生成器函數(shù)。
5. 反向迭代
反方向迭代一個(gè)序列十绑,需要使用reversed()
函數(shù)聚至。
>>> a = [1, 2, 3, 4]
>>> for x in reversed(a):
... print(x)
...
4
3
2
1
反向迭代需要兩個(gè)條件:
當(dāng)對(duì)象的大小可預(yù)先確定
對(duì)象實(shí)現(xiàn)了
__reversed__()
的特殊方法時(shí)才能生效。
如果兩者都不符合本橙,那你必須先將對(duì)象轉(zhuǎn)換為一個(gè) list
才行扳躬。
>>> f = open('/Users/faris/Desktop/docker.txt')
>>>
>>> for line in reversed(list(f)):
... print(line, end='')
在自定義類上實(shí)現(xiàn) __reversed__()
方法來(lái)實(shí)現(xiàn)反向迭代,會(huì)使代碼更加的高效勋功,因?yàn)椴挥迷俎D(zhuǎn)化成 list
后再反向迭代坦报。
class Countdown:
def __init__(self, start):
self.start = start
def __iter__(self):
n = self.start
while n > 0 :
yield n
n = n - 1
def __reversed__(self):
n = 0
while n <= self.start:
yield n
n = n + 1
>>> a=Countdown(10)
>>> [n for n in a]
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
>>>
>>> [n for n in reversed(a)]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
6. 迭代器切片
標(biāo)準(zhǔn)的切片操作,需要知道所切對(duì)象的長(zhǎng)度狂鞋,因此迭代器與生成器不能使用標(biāo)準(zhǔn)切片片择。
函數(shù) itertools.islice()
正好適用于在迭代器和生成器上做切片操作
>>> a=[10, 20, 30]
>>>
>>> a[1:]
[20, 30]
>>>
>>> b=(n for n in a)
>>>
>>> b[1:]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'generator' object is not subscriptable
>>>
>>> b=(n for n in a)
>>> for x in itertools.islice(b, 1, 2):
... print(x)
...
20
7. 跳過(guò)可迭代對(duì)象的開始部分
你想遍歷一個(gè)可迭代對(duì)象,但是它開始的某些元素你并不感興趣骚揍,想跳過(guò)它們字管。
itertools.dropwhile()
函數(shù),你給它傳遞一個(gè)函數(shù)對(duì)象和一個(gè)可迭代對(duì)象信不,它會(huì)返回一個(gè)迭代器對(duì)象嘲叔。
>>> from itertools import dropwhile
>>>
>>> with open('/etc/passwd') as f:
... for line in dropwhile(lambda line: line.startswith('#'), f):
... print(line, end='')
如果想跳過(guò)前幾個(gè)元素的話,可以使用上一節(jié)的 itertools.islice()
>>> from itertools import islice
>>>
>>> a=[10, 20, 30, 40, 50]
>>> for n in islice(a, 3, None):
... print(n)
...
40
50
或者我們?nèi)匀豢梢允褂蒙善鱽?lái)完成抽活。
>>> with open('/etc/passwd') as f:
... lines = (line for line in f if not line.startswith('#'))
... for line in lines:
... print(line, end='')
8. 排列組合的迭代
迭代遍歷一個(gè)集合中元素的所有可能的排列或組合硫戈。
8.1 itertools.permutations()
可以得到輸入序列的所有排列組合
>>> from itertools import permutations
>>>
>>> items = ['a', 'b', 'c']
>>> for p in permutations(items):
... print(p)
...
('a', 'b', 'c')
('a', 'c', 'b')
('b', 'a', 'c')
('b', 'c', 'a')
('c', 'a', 'b')
('c', 'b', 'a')
也可以選擇可排列的個(gè)數(shù)
>>> from itertools import permutations
>>>
>>> items = ['a', 'b', 'c']
>>> for p in permutations(items, 2):
... print(p)
...
('a', 'b')
('a', 'c')
('b', 'a')
('b', 'c')
('c', 'a')
('c', 'b')
8.2 itertools.combinations()
可以得到輸入序列的所有組合。(不關(guān)心順序)
>>> from itertools import combinations
>>>
>>> items = ['a', 'b', 'c']
>>> for p in combinations(items, 2):
... print(p)
...
('a', 'b')
('a', 'c')
('b', 'c')
8.3 itertools.combinations_with_replacement()
在組合時(shí)下硕,允許同一元素在不同位置重復(fù)出現(xiàn)
>>> from itertools import combinations_with_replacement
>>>
>>> items = ['a', 'b', 'c']
>>> for p in combinations_with_replacement(items, 2):
... print(p)
...
('a', 'a')
('a', 'b')
('a', 'c')
('b', 'b')
('b', 'c')
('c', 'c')
9. 序列上索引值迭代
你想在迭代一個(gè)序列的同時(shí)跟蹤正在被處理的元素索引丁逝。
內(nèi)置的 enumerate()
函數(shù)可以很好的解決這個(gè)問(wèn)題:
>>> my_list = ['a', 'b', 'c']
>>>
>>> for n in enumerate(my_list):
... print(n)
...
(0, 'a')
(1, 'b')
(2, 'c')
你可以傳遞一個(gè)開始索引參數(shù):
>>> my_list = ['a', 'b', 'c']
>>>
>>> for n in enumerate(my_list, 2):
... print(n)
...
(2, 'a')
(3, 'b')
(4, 'c')
這樣在你處理錯(cuò)誤是很好定位的。
def parse_data(filename):
with open(filename, 'rt') as f:
for lineno, line in enumerate(f, 1):
fields = line.split()
try:
count = int(fields[1])
except ValueError as e:
print('Line {}: Parse error: {}'.format(lineno, e))
另外需要注意:
序列元素本身就是元組的時(shí)候梭姓,取值應(yīng)該是這樣子的霜幼。
>>> data = [ (1, 2), (3, 4), (5, 6), (7, 8) ]
>>>
>>> for no, (x, y) in enumerate(data):
... print(no, x, y)
...
0 1 2
1 3 4
2 5 6
3 7 8
10. 展開嵌套的序列
你想將一個(gè)多層嵌套的序列展開成一個(gè)單層列表。
我們可以寫一個(gè)包含 yield from
語(yǔ)句的遞歸生成器來(lái)輕松解決這個(gè)問(wèn)題:
>>> from collections import Iterable
>>>
>>> def flatten(items, ignore_types=(str, bytes)):
... for n in items:
... if isinstance(n, Iterable) and not isinstance(n, ignore_types):
... yield from flatten(n)
... else:
... yield n
...
>>>
>>> items = [1, 2, [3, 4, [5, 6], 7], 8, 'faris']
>>>
>>> print([n for n in flatten(items)])
[1, 2, 3, 4, 5, 6, 7, 8, 'faris']
isinstance
用來(lái)判斷類型誉尖。
11. 順序迭代合并后的排序迭代對(duì)象
你有一系列排序序列罪既,想將它們合并后得到一個(gè)排序序列并在上面迭代遍歷。
可以使用 heapq.merge
。由于它不會(huì)立即合并琢感,所以我們可以在很長(zhǎng)的序列上使用它丢间,也不會(huì)帶來(lái)很大的開銷。
但是要求所有序列必須是排過(guò)序的驹针。
>>> import heapq
>>> a = [1, 4, 7, 10]
>>> b = [2, 5, 6, 11]
>>>
>>> for n in heapq.merge(a, b):
... print(n)
...
1
2
4
5
6
7
10
11
如果有一個(gè)序列沒有排序千劈,它不會(huì)檢查,則會(huì)直接輸出:
>>> import heapq
>>> a = [1, 4, 7, 10]
>>> b = [ 6, 11, 2, 5]
>>>
>>> for n in heapq.merge(a, b):
... print(n)
...
1
4
6
7
10
11
2
5