迭代器與生成器(cookbook筆記)

迭代器與生成器

手動遍歷迭代器

  • 不使用for循環手動遍歷迭代器
def manual_iter():
    '''
    手動遍歷迭代器
    '''
    with open('/etc/passwd') as f:
        try:
            while True:
                line = next(f)
                print (line)
        #通過StopIteration異常來標記結尾
        except StopIteration:
            pass

def manual_iter_1():
    with open('/etc/passwd') as f:
        while True:
            line = next(f, None)
            if line is None:
                break
            print (line)

代理迭代

  • 在一個新的容器(class)中迭代對象
class Node:
    def __init__(self, value):
        self._value = value
        #存儲Node對象
        self._children = []

    def __repr__(self):
        return 'Node({!r})'.format(self._value)

    def add_child(self, node):
        self._children.append(node)

    def __iter__(self):
        '''
        傳遞迭代請求給內部的_children,實現了一個__next()__方法的迭代器對象
        '''
        return iter(self._children)

if __name__ == '__main__':
    root = Node(0)
    child1 = Node(1)
    child2 = Node(2)
    root.add_child(child1)
    root.add_child(child2)
    for ch in root:
        print (ch)

使用生成器創建新的迭代模式

  • 生成器只能用于迭代操做,使用for循環迭代生成器會自動處理StopIteration異常
def frange(start, stop, increment):
    x = start
    while x < stop:
        yield x
        x += increment

def countdown(n):
    print ('Starting to count from', n)
    while n > 0:
        yield n
        n -= 1
    print ('Done!')

實現迭代器協議

  • 構建一個支持迭代操作的自定義對象
class Node:
    def __init__(self, value):
        self._value = value
        #存儲Node對象
        self._children = []

    def __repr__(self):
        return 'Node({!r})'.format(self._value)

    def add_child(self, node):
        self._children.append(node)

    def __iter__(self):
        #傳遞迭代請求給內部的_children
        return iter(self._children)
        #return self._children.__iter__()

    def depth_first(self):
        #返回自己本身的一個迭代
        yield self
        for c in self:
            #調用迭代本身的函數
            yield from c.depth_first()

if __name__ == '__main__':
    #根
    root = Node(0)
    child1 = Node(1)
    child2 = Node(2)
    root.add_child(child1)
    root.add_child(child2)
    child1.add_child(Node(3))
    child1.add_child(Node(4))
    child2.add_child(Node(5))
    for ch in root.depth_first():
        print (ch)

反向迭代

  • 使用內置的reversed()
  • 反向迭代僅僅當對象的大小可預先確定或者對象實現了 reversed() 的特殊方法時才能生效
>>> a = [1, 2, 3, 4]
>>> for x in reversed(a):
...     print (x)
4
3
2
1
  • 通過reversed自定義類的反向迭代
class Countdown:

    def __init__(self, start):
        self.start = start

    def __iter__(self):
        n = self.start
        while n > 0:
            yield n
            n -= 1

    def __reversed__(self):
        n = 1
        while n <= self.start:
            yield n
            n += 1

if __name__ == '__main__':
    #調用自己實現的__reversed__方法
    for rr in reversed(Countdown(30)):
        print (rr)
    for rr in Countdown(30):
        print (rr)

帶有外部狀態的生成器函數

  • 定義一個生成器函數,調用某個你想暴露給用戶使用的外部狀態值
  • 通過類的iter函數實現
class LineHistory:
    def __init__(self, lines, histlen=3):
        self.lines = lines
        self.history = deque(maxlen=histlen)

    def __iter__(self):
        #遍歷并標記計數起始點
        for lineno, line in enumerate(self.lines, 1):
            self.history.append((lineno, line))
            yield line

    def clear(self):
        self.history.clear()

if __name__ == '__main__':
    with open('ch4_1.py') as f:
        lines = LineHistory(f)
        #__iter_函數返回的生成器 
        for line in lines:
            if 'python' in line:
                for lineno, hline in lines.history:
                    print ('{}:{}'.format(lineno, hline))

迭代器切片

  • 得到一個由迭代器生成的切片對象
  • 迭代器和生成器不能使用標準的切片操作,因為它們的長度事先我們并不知道(并且也沒有實現索引)。 函數 islice() 返回一個可以生成指定元素的迭代器,它通過遍歷并丟棄直到切片開始索引位置的所有元素。 然后才開始一個個的返回元素,并直到切片結束索引位置。
  • islice() 會消耗掉傳入的迭代器中的數據
import itertools
def count(n):
    while True:
        yield n
        n += 1

if __name__ == '__main__':
    c = count(0)
    for x in itertools.islice(c, 10, 20):
        print (x)

跳過可迭代對象的開始部分

  • 跳過具體部分(開頭是#的文件)
from itertools import dropwhile
def open_passwd():
    with open('/etc/passwd') as f:
        for line in dropwhile(lambda line:line.startswith('#'), f):
            print (line)
  • 如果明確知道跳過的個數
>>> from itertools import islice
>>> items = ['a', 'b', 'c', 1, 2, 3]
>>> for x in islice(items, 3, None):
...     print (x)
1
2
3

排列組合迭代

  • 迭代遍歷一個集合中元素的所有可能的排列或組合
  • permutations接受一個集合并產生一個元組序列,每個元組由集合中所有元素的一個可能排列組成
>>> items = ['a', 'b', 'c']
>>> from itertools import permutations
>>> 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')

  • 指定長度的排列
>>> for p in permutations(items, 2):
...     print (p)
('a', 'b')
('a', 'c')
('b', 'a')
('b', 'c')
('c', 'a')
('c', 'b')
  • 指定長度的組合(不考慮排序)
>>> from itertools import combinations
>>> for c in combinations(items, 3):
...     print (c)
('a', 'b', 'c')
>>> for c in combinations(items, 2):
...     print (c)
('a', 'b')
('a', 'c')
('b', 'c')

序列上索引值迭代

  • enumerate()可以很好解決
  • 指定一個start,改變索引開始的值
>>> for id, val in enumerate(my_list, 1):
...     print (id, val)
...     
... 
1 a
2 b
3 c
  • 在遍歷文件時定位錯誤行數
def pares_data(filename):
    with open(filename, 'r') as f:
        for lineno, line in enumerate(f, 1):
            fields = line.split()
            try:
                count = int(fields[0])
            except ValueError as e:
                print ('Line {}: Parse error:{}'.format(lineno, e))
  • 跟蹤某些單詞在出現的函數,構造出一個字典
def line_dict():
    from collections import defaultdict
    #初始化字典,value=list
    word_summary = defaultdict(list)
    with open('ch4_1.py', 'r') as f:
        lines = f.readlines()

    for idx, line in enumerate(lines):
        words = [w.strip().lower() for w in line.split()]
        for word in words:
            word_summary[word].append(idx)
    return word_summary

同時迭代多個序列

  • 使用zip(), 迭代的次數和最短的序列一致
>>> xpts = [1, 5, 4, 2, 10, 7]
>>> ypts = [101, 78, 37, 15, 52, 99]
>>> for x, y in zip(xpts, ypts):
...     print (x, y)
1 101
5 78
4 37
2 15
10 52
7 99
  • 可以使用itertools.zip_longest()來填充沒有zip迭代到的默認值
>>> for i in zip_longest(a, b):
...     print (i)
(1, 'w')
(2, 'x')
(3, 'y')
(None, 'z')
>>> for i in zip_longest(a, b, fillvalue=0):
...     print (i)
(1, 'w')
(2, 'x')
(3, 'y')
(0, 'z')
  • 如果zip的列表一個是key,一個是value,可以打包成字典
>>> headers = ['name', 'shares', 'price']
>>> values = ['ACME', 100, 490.1]
>>> s = dict(zip(headers, values))
>>> print (s) {'name': 'ACME', 'shares': 100, 'price': 490.1}

不同集合上元素的迭代

  • 在不同的可迭代對象上循環變量(避免寫重復的for循環)
>>> from itertools import chain
>>> a = [1, 2, 3, 4]
>>> b = ['x', 'y', 'z']
>>> for x in chain(a, b):
...     print (x)
1
2
3
4
x
y
z

數據處理管道

import os
import fnmatch
import gzip
import bz2
import re

def gen_find(filepat, top):
    #遍歷當期文件夾的所有內容,返回當期文件夾路徑,文件夾列表,文件列表
    for path, dirlist, filelist in os.walk(top):
        #找到匹配的日志,重新組裝路徑
        for name in fnamatch.filter(filelist, filepat):
            yield os.path.join(path, name)

def gen_opener(filenames):
    #每次返回一個打開的文件描述符
    for filename in filenames:
        if filename.endswith('.gz'):
            f = gzip.open(filename, 'tr')
        elif filename.endswith('.bz2'):
            f = bz2.open(filename, 'rt')
        else:
            f = open(filename, 'rt')
        yield f
        f.close()

def gen_concatenate(iterators):
    #拼接打開的內容
    for it in iterators:
        yield from it

def gen_grep(pattern, lines):
    pat = re.complate(pattern)
    for line in lines:
        if pat.search(line):
            yield line

if __name__ == '__main__':
    lognames = gen_find('access-log*', 'www')
    files = gen_opener(lognames)
    pylines = gen_grep('(?i)python', lines)
    for line in pylines:
        print (lines)

展開嵌套的序列

  • 將一個多層嵌套序列展開為單層列表
  • Iterable檢查元素是否是可以迭代的,ignore_types防止迭代字符數據
from collections import Iterable
def flatten(items, ignore_types=(str, bytes)):
    for x in items:
        if isinstance(x, Iterable) and not isinstance(x, ignore_types):
            yield from flatten(x)
        else:
            yield x

if __name__ == '__main__':
    items = [1, 2, [3, 4, [5, 6], 7], 8]
    for x in flatten(items):
        print (x)

順序迭代合并后的序列

  • 要求輸入序列是經過排序的
>>> import heapq
>>> a  = [1, 4, 7, 10]
>>> b = [2, 5, 6, 11]
>>> for c in heapq.merge(a, b):
...     print (c)
1
2
4
5
6
7
10
11
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,786評論 6 534
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,656評論 3 419
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,697評論 0 379
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,098評論 1 314
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,855評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,254評論 1 324
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,322評論 3 442
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,473評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,014評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,833評論 3 355
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,016評論 1 371
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,568評論 5 362
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,273評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,680評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,946評論 1 288
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,730評論 3 393
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,006評論 2 374

推薦閱讀更多精彩內容