淺析Python自帶性能強(qiáng)悍的標(biāo)準(zhǔn)庫itertools
前言
?
可迭代對(duì)象就像密閉容器里的水,有貨倒不出
itertools是python內(nèi)置的標(biāo)準(zhǔn)模塊,提供了很多簡潔又高效的專用功能,使用得當(dāng)能夠極大的簡化代碼行數(shù),同時(shí)所有方法都是實(shí)現(xiàn)了生成器函數(shù),這就意味著極大的節(jié)省內(nèi)存。
itertools提供的功能主要分為三大塊,以最新版本的3.10為例:
- 對(duì)可迭代對(duì)象無限迭代,無限輸出
- 對(duì)可迭代對(duì)象有限迭代
- 對(duì)可迭代對(duì)象排列組合
方法如下:
導(dǎo)入包
>>> from iteratortools import *
無限迭代
iteratortools.count(start=0, step=1)
數(shù)值生成器,可以指定起始位置和步長,并且步長可以為浮點(diǎn)數(shù)。無限輸出,一直累加,在例子中需要邊睡眠1s邊輸出。
>>> import time >>> iterator = count(4, 0.5) >>> for i in iterator: ... print(i) ... time.sleep(1) ... 4 4.5 5.0 5.5 6.0 6.5 7.0 7.5
iteratortools.cycle(iteratorable)
無限循環(huán)取出可迭代對(duì)象里的元素
>>> a = cycle("ABCD") >>> import time >>> for i in a: ... print(i) ... time.sleep(1) ... A B C D A B C D
iteratortools.repeat(object[, times])
不斷重復(fù)輸出整個(gè)object,如果指定了重復(fù)次數(shù),則輸出指定次數(shù),否則將無限重復(fù)。
>>> iterator = repeat('hello world', 10) >>> >>> for i in iterator: ... print(i) ... hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world
有了這個(gè)神器,對(duì)輸出10次hello world這種問題又有一種新解法
有限迭代
iteratortools.accumulate(iteratorable[, func, *, initial=None])
返回對(duì)列表中元素逐項(xiàng)的操作,操作有:
- 累加,返回累加到每一項(xiàng)的列表
- 累乘,返回累乘到每一項(xiàng)的列表
- 最小值,返回到當(dāng)前項(xiàng)的最小值
- 最大值,返回到當(dāng)前項(xiàng)的最大值
>>> [2, 4, 8, 1, 3, 5] [2, 4, 8, 1, 3, 5] >>> arr = [2, 4, 8, 1, 3, 5] >>> >>> add = accumulate(arr) >>> >>> list(add) [2, 6, 14, 15, 18, 23] >>> >>> max = accumulate(arr, max) >>> list(max) [2, 4, 8, 8, 8, 8] >>> >>> import operator >>> mul = accumulate(arr, operator.mul) >>> list(mul) [2, 8, 64, 64, 192, 960] >>> >>> min = accumulate(arr, min) >>> list(min) [2, 2, 2, 1, 1, 1]
iteratortools.chain(*iteratorables)
將多個(gè)可迭代對(duì)象構(gòu)建成一個(gè)新的可迭代對(duì)象,統(tǒng)一返回。類似于將多個(gè)對(duì)象鏈成一條串
>>> iterator = chain([1,2,3],['a','b','c'],(5,6,7)) >>> list(iterator) [1, 2, 3, 'a', 'b', 'c', 5, 6, 7]
優(yōu)點(diǎn):可以將多個(gè)可迭代對(duì)象整合成一個(gè),避免逐個(gè)取值
chain.from_iteratorable(iteratorable)
將一個(gè)迭代對(duì)象中將所有元素類似于chain一樣,統(tǒng)一返回。
>>> chain.from_iteratorable(['abc','def']) <iteratortools.chain object at 0x1083ae460> >>> iterator = chain.from_iteratorable(['abc','def']) >>> list(iterator) ['a', 'b', 'c', 'd', 'e', 'f']
iteratortools.compress(data, selectors)
按照真值表篩選元素
>>> arr = [1,2,3,4] >>> selectors = [1,0,1,0] >>> >>> iterator = compress(arr, selectors) >>> >>> list(iterator) [1, 3]
iteratortools.dropwhile(predicate, iteratorable)
按照條件篩選,丟棄掉第一次不符合條件時(shí)之前的所有元素
>>> arr = [1,2,3,2,1,2,1] >>> iterator = dropwhile(lambda x: x<3, arr) >>> list(iterator) [3, 2, 1, 2, 1]
iteratortools.takewhile(predicate, iteratorable)
根據(jù)predicate條件篩選可迭代對(duì)象中的元素,只要元素為真就返回,第一次遇到不符合的條件就退出。
按照條件篩選,丟棄第一次遇到不符合條件之后的元素。行為類似于上一個(gè)dropwhile,區(qū)別在于丟棄的選擇不同。
iteratortools.filterfalse(predicate, iteratorable)
保留不符合條件的元素,返回迭代器
>>> arr = [1,2,3,4,5] >>> iterator = filterfalse(lambda x:x<3, arr) >>> list(iterator) [3, 4, 5]
iteratortools.groupby(iteratorable, key=None)
按照指定的條件分類。輸出條件和符合條件的元素
>>> iterator = groupby(arr, lambda x: x>3) >>> for condition ,numbers in iterator: ... print(condition, list(numbers)) ... False [1, 2, 3] True [4, 5]
iteratortools.islice(iteratorable, start, stop[, step])
對(duì)迭代器進(jìn)行切片,老版本中不能指定start和stop以及步長,新版本可以。
>>> iterator = count() >>> slice_iterator = islice(iterator, 10, 20, 2) >>> list(slice_iterator) [10, 12, 14, 16, 18]
iteratortools.starmap(function, iteratorable)
將function作用于可迭代對(duì)象上,類似于map函數(shù)
iteratortools.tee(iteratorable, n=2)
從一個(gè)可迭代對(duì)象中返回 n 個(gè)獨(dú)立的迭代器
>>> iterator = tee(arr) >>> for i in iterator: ... print(type(i), list(i)) ... <class 'iteratortools._tee'> [1, 2, 3, 4, 5] <class 'iteratortools._tee'> [1, 2, 3, 4, 5]
iteratortools.zip_longest(*iteratorables, fillvalue=None)
創(chuàng)建一個(gè)迭代器,從每個(gè)可迭代對(duì)象中收集元素。如果可迭代對(duì)象的長度未對(duì)齊,將根據(jù) fillvalue 填充缺失值。
迭代持續(xù)到耗光最長的可迭代對(duì)象。大致相當(dāng)于:
>>> iterator = zip_longest("ABCD", "xy", fillvalue="-") >>> list(iterator) [('A', 'x'), ('B', 'y'), ('C', '-'), ('D', '-')]
排列組合迭代
iteratortools.product(*iteratorables, repeat=1)
生成多個(gè)可迭代對(duì)象的笛卡爾積
大致相當(dāng)于生成器表達(dá)式中的嵌套循環(huán)。例如, product(A, B) 和 ((x,y) for x in A for y in B) 返回結(jié)果一樣。
>>> iterator = product("123", "abc") >>> list(iterator) [('1', 'a'), ('1', 'b'), ('1', 'c'), ('2', 'a'), ('2', 'b'), ('2', 'c'), ('3', 'a'), ('3', 'b'), ('3', 'c')]
將可選參數(shù) repeat 設(shè)定為要重復(fù)的次數(shù)。例如,product(A, repeat=4) 和 product(A, A, A, A) 是一樣的
iteratortools.permutations(iteratorable, r=None)
由 iteratorable 元素生成長度為 r 的排列。元素的排列,類似于給一個(gè)[1,2,3],選取其中兩個(gè)元素,一共有多少種組合方法?不要求元素排列之后的位置。
>>> iter = permutations([1,2,3], r=3) >>> list(iterator) [(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]
這個(gè)方法能夠完美解決算法中的全排列問題,簡直是量身定做。如果早知道這么簡單,當(dāng)年考算法也不會(huì)。。,哎
可參見leetcode46題: 力扣
iteratortools.combinations(iteratorable, r)
返回由輸入 iteratorable 中元素組成長度為 r 的子序列。元素不可重復(fù)使用。子序列是要求元素在排列之后和之前的相對(duì)位置不變的。1,2,3中3在1的后面,子序列中3也一定在1的后面。
>>> iterator = combinations([1,2,3,4], r = 3) >>> list(iterator) [(1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4)] >>> iterator = combinations([1], r = 3) >>> list(iterator) []
這個(gè)方法可以曲線解決組合總數(shù)問題
iteratortools.combinations_with_replacement(iteratorable, r)
返回由輸入 iteratorable 中元素組成的長度為 r 的子序列,允許每個(gè)元素可重復(fù)出現(xiàn)
>>> iter = combinations_with_replacement([1,2,3,4], r=2) >>> list(iter) [(1, 1), (1, 2), (1, 3), (1, 4), (2, 2), (2, 3), (2, 4), (3, 3), (3, 4), (4, 4)] >>> iterator = combinations_with_replacement([1], r=3) >>> list(iterator) [(1, 1, 1)]
到此這篇關(guān)于淺析Python自帶性能強(qiáng)悍的標(biāo)準(zhǔn)庫itertools的文章就介紹到這了,更多相關(guān)Python 標(biāo)準(zhǔn)庫itertools內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python中itertools簡介使用介紹
- python迭代器模塊itertools常用的方法
- Python中itertools模塊的使用教程詳解
- Python中itertools庫的四個(gè)函數(shù)介紹
- python中itertools模塊使用小結(jié)
- 關(guān)于Python 內(nèi)置庫 itertools
- Python函數(shù)式編程中itertools模塊詳解
- Python編程itertools模塊處理可迭代集合相關(guān)函數(shù)
- 關(guān)于Python中 循環(huán)器 itertools的介紹
- 詳解python itertools功能
- python排列組合庫itertools的具體使用
相關(guān)文章
python實(shí)現(xiàn)截取屏幕保存文件,刪除N天前截圖的例子
今天小編就為大家分享一篇python實(shí)現(xiàn)截取屏幕保存文件,刪除N天前截圖的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-08-08Python+Opencv實(shí)現(xiàn)圖像模板匹配詳解
模板匹配可以看作是對(duì)象檢測(cè)的一種非常基本的形式。使用模板匹配,我們可以使用包含要檢測(cè)對(duì)象的“模板”來檢測(cè)輸入圖像中的對(duì)象。本文為大家介紹了圖像模板匹配的實(shí)現(xiàn)方法,需要的可以參考一下2022-09-09python實(shí)現(xiàn)人人網(wǎng)登錄示例分享
這篇文章主要介紹了python實(shí)現(xiàn)登錄人人網(wǎng)示例,大家參考使用吧2014-01-01Django 使用easy_thumbnails壓縮上傳的圖片方法
今天小編就為大家分享一篇Django 使用easy_thumbnails壓縮上傳的圖片方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-07-07淺談django url請(qǐng)求與數(shù)據(jù)庫連接池的共享問題
今天小編就為大家分享一篇淺談django url請(qǐng)求與數(shù)據(jù)庫連接池的共享問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-08-08