python中itertools模塊使用小結
Python 內置的 itertools 模塊包含了一系列用來產生不同類型迭代器的函數(shù)或類,這些函數(shù)的返回都是一個迭代器,我們可以通過 for 循環(huán)來遍歷取值,也可以使用 next() 來取值。
itertools 模塊提供的迭代器函數(shù)有以下幾種類型:
- 無限迭代器:生成一個無限序列,比如自然數(shù)序列 1, 2, 3, 4, ...;
- 有限迭代器:接收一個或多個序列(sequence)作為參數(shù),進行組合、分組和過濾等;
- 組合生成器:序列的排列、組合,求序列的笛卡兒積等。
itertools
高效循環(huán)下創(chuàng)建循環(huán)器的標準庫
Infinite itertools,無限迭代器
itertools.count(start=0, step=10)
默認返回一個從0開始,依次+10的自然數(shù)迭代器,如果你不停止,它會一直運行下去。
可以用start指定開始的位置,step是步長。
import itertools c = itertools.count(start=0, step=1) for i in c: print(i) # 0 # 10 # 20 # 30 # 40 # 50 # ...
itertools.cycle(iterable)
傳入一個可迭代對象,然后無限循環(huán)迭代。
import itertools # itertools.count() l = [1,2,3,4,5] c = itertools.cycle(l) for i in c: print(i) # 1 # 2 # 3 # 4 # 5 # 1 # 2 # 3 # 4 # 5 # ...
itertools.repeat(p_object, times=None)
重復迭代一個對象p_object,如果不指定times,則會迭代無數(shù)次,也可以通過times參數(shù)指定迭代的次數(shù)。
import itertools # itertools.count() l = [1,2,3,4,5] c = itertools.repeat(l, 5) for i in c: print(i) # [1, 2, 3, 4, 5] # [1, 2, 3, 4, 5] # [1, 2, 3, 4, 5] # [1, 2, 3, 4, 5] # [1, 2, 3, 4, 5]
Iterators terminating on the shortest input sequence
itertools.accumulate(iterable, func)
返回序列的累計值或者其他二進制函數(shù)。
import itertools # itertools.count() l = [1,2,3,4,5] c = itertools.accumulate(l) print(c) for i in c: print(i) # 1 # 3 # 6 # 10 # 15
accumulate()仍然返回的是一個迭代器,傳一個list,在for循環(huán)中遍歷打印的時候發(fā)現(xiàn),它做了累加操作。(迭代第一個數(shù),就是前一個數(shù)的和,迭代到第二個數(shù)時,就是前兩個數(shù)的和,以此類推)
并且做遞加操作時支持:list, tuple, str, set, dict
傳入的是dict對象,那么會累加迭代dict的key:
import itertools # itertools.count() d = {'a': 1, 'b': 2, 'c': 3} c = itertools.accumulate(d) print(c) for i in c: print(i) # <itertools.accumulate object at 0x000001F7A0A90E48> # a # ab # abc
itertools.chain(*iterables)
chain()方法中的參數(shù)可以傳入多個序列,而且只要是序列即可,不限定序列的數(shù)據類型。
如:迭代list, tuple, str三個序列
import itertools l = [1, 2, 3, 4, 5] t = (1, 2, 3, 4, 5) s = 'abcdefg' c = itertools.chain(l, t, s) print(c) for i in c: print(i) # <itertools.chain object at 0x0000026E64801448> # 1 # 2 # 3 # 4 # 5 # 1 # 2 # 3 # 4 # 5 # a # b # c # d # e # f # g
itertools 笛卡爾積
import itertools d = [ [{"a": 1}, {"b": 2}], [{"c": 3}, {"d": 4}, {"e": 5}], [{"f": 6}, {"g": 7}] ] print(*d) for i in itertools.product(*d): print(i) # [{'a': 1}, {'b': 2}] [{'c': 3}, {'d': 4}, {'e': 5}] [{'f': 6}, {'g': 7}] # ({'a': 1}, {'c': 3}, {'f': 6}) # ({'a': 1}, {'c': 3}, {'g': 7}) # ({'a': 1}, {'d': 4}, {'f': 6}) # ({'a': 1}, {'d': 4}, {'g': 7}) # ({'a': 1}, {'e': 5}, {'f': 6}) # ({'a': 1}, {'e': 5}, {'g': 7}) # ({'b': 2}, {'c': 3}, {'f': 6}) # ({'b': 2}, {'c': 3}, {'g': 7}) # ({'b': 2}, {'d': 4}, {'f': 6}) # ({'b': 2}, {'d': 4}, {'g': 7}) # ({'b': 2}, {'e': 5}, {'f': 6}) # ({'b': 2}, {'e': 5}, {'g': 7})
到此這篇關于python中的itertools模塊簡單使用的文章就介紹到這了,更多相關python itertools模塊使用內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python英文詞頻統(tǒng)計(哈姆雷特)程序示例代碼
在文本處理方面,Python也有著得天獨厚的優(yōu)勢,不僅提供了多種字符串操作函數(shù),而且還可以使用各種開源庫來處理文本,下面這篇文章主要給大家介紹了關于Python英文詞頻統(tǒng)計(哈姆雷特)程序示例的相關資料,需要的朋友可以參考下2023-06-06Python while、for、生成器、列表推導等語句的執(zhí)行效率測試
這篇文章主要介紹了Python while、for、生成器、列表推導等語句的執(zhí)行效率測試,本文分別用兩段程序測算出了各語句的執(zhí)行效率,然后總結了什么情況下使用什么語句優(yōu)先使用的語句等,需要的朋友可以參考下2015-06-06解決Pytorch自定義層出現(xiàn)多Variable共享內存錯誤問題
這篇文章主要介紹了解決Pytorch自定義層出現(xiàn)多Variable共享內存錯誤問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06Python爬蟲beautifulsoup4常用的解析方法總結
今天小編就為大家分享一篇關于Python爬蟲beautifulsoup4常用的解析方法總結,小編覺得內容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-02-02