python中itertools模塊使用小結(jié)
Python 內(nèi)置的 itertools 模塊包含了一系列用來(lái)產(chǎn)生不同類型迭代器的函數(shù)或類,這些函數(shù)的返回都是一個(gè)迭代器,我們可以通過(guò) for 循環(huán)來(lái)遍歷取值,也可以使用 next() 來(lái)取值。
itertools 模塊提供的迭代器函數(shù)有以下幾種類型:
- 無(wú)限迭代器:生成一個(gè)無(wú)限序列,比如自然數(shù)序列 1, 2, 3, 4, ...;
- 有限迭代器:接收一個(gè)或多個(gè)序列(sequence)作為參數(shù),進(jìn)行組合、分組和過(guò)濾等;
- 組合生成器:序列的排列、組合,求序列的笛卡兒積等。
itertools
高效循環(huán)下創(chuàng)建循環(huán)器的標(biāo)準(zhǔn)庫(kù)
Infinite itertools,無(wú)限迭代器
itertools.count(start=0, step=10)
默認(rèn)返回一個(gè)從0開始,依次+10的自然數(shù)迭代器,如果你不停止,它會(huì)一直運(yùn)行下去。
可以用start指定開始的位置,step是步長(zhǎng)。
import itertools c = itertools.count(start=0, step=1) for i in c: print(i) # 0 # 10 # 20 # 30 # 40 # 50 # ...
itertools.cycle(iterable)
傳入一個(gè)可迭代對(duì)象,然后無(wú)限循環(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)
重復(fù)迭代一個(gè)對(duì)象p_object,如果不指定times,則會(huì)迭代無(wú)數(shù)次,也可以通過(guò)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)
返回序列的累計(jì)值或者其他二進(jìn)制函數(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()仍然返回的是一個(gè)迭代器,傳一個(gè)list,在for循環(huán)中遍歷打印的時(shí)候發(fā)現(xiàn),它做了累加操作。(迭代第一個(gè)數(shù),就是前一個(gè)數(shù)的和,迭代到第二個(gè)數(shù)時(shí),就是前兩個(gè)數(shù)的和,以此類推)
并且做遞加操作時(shí)支持:list, tuple, str, set, dict
傳入的是dict對(duì)象,那么會(huì)累加迭代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ù)可以傳入多個(gè)序列,而且只要是序列即可,不限定序列的數(shù)據(jù)類型。
如:迭代list, tuple, str三個(gè)序列
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})
到此這篇關(guān)于python中的itertools模塊簡(jiǎn)單使用的文章就介紹到這了,更多相關(guān)python itertools模塊使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python中itertools簡(jiǎn)介使用介紹
- python迭代器模塊itertools常用的方法
- Python中itertools模塊的使用教程詳解
- Python中itertools庫(kù)的四個(gè)函數(shù)介紹
- 淺析Python自帶性能強(qiáng)悍的標(biāo)準(zhǔn)庫(kù)itertools
- 關(guān)于Python 內(nèi)置庫(kù) itertools
- Python函數(shù)式編程中itertools模塊詳解
- Python編程itertools模塊處理可迭代集合相關(guān)函數(shù)
- 關(guān)于Python中 循環(huán)器 itertools的介紹
- 詳解python itertools功能
- python排列組合庫(kù)itertools的具體使用
相關(guān)文章
GDAL 矢量屬性數(shù)據(jù)修改方式(python)
這篇文章主要介紹了GDAL 矢量屬性數(shù)據(jù)修改方式(python),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03Python英文詞頻統(tǒng)計(jì)(哈姆雷特)程序示例代碼
在文本處理方面,Python也有著得天獨(dú)厚的優(yōu)勢(shì),不僅提供了多種字符串操作函數(shù),而且還可以使用各種開源庫(kù)來(lái)處理文本,下面這篇文章主要給大家介紹了關(guān)于Python英文詞頻統(tǒng)計(jì)(哈姆雷特)程序示例的相關(guān)資料,需要的朋友可以參考下2023-06-06Python while、for、生成器、列表推導(dǎo)等語(yǔ)句的執(zhí)行效率測(cè)試
這篇文章主要介紹了Python while、for、生成器、列表推導(dǎo)等語(yǔ)句的執(zhí)行效率測(cè)試,本文分別用兩段程序測(cè)算出了各語(yǔ)句的執(zhí)行效率,然后總結(jié)了什么情況下使用什么語(yǔ)句優(yōu)先使用的語(yǔ)句等,需要的朋友可以參考下2015-06-06Python給exe添加以管理員運(yùn)行的屬性方法詳解
這篇文章主要為大家介紹了Python給exe添加以管理員運(yùn)行的屬性方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12解決Pytorch自定義層出現(xiàn)多Variable共享內(nèi)存錯(cuò)誤問題
這篇文章主要介紹了解決Pytorch自定義層出現(xiàn)多Variable共享內(nèi)存錯(cuò)誤問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-06-06Python爬蟲beautifulsoup4常用的解析方法總結(jié)
今天小編就為大家分享一篇關(guān)于Python爬蟲beautifulsoup4常用的解析方法總結(jié),小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-02-02