淺析Python自帶性能強(qiáng)悍的標(biāo)準(zhǔn)庫(kù)itertools
前言
?
可迭代對(duì)象就像密閉容器里的水,有貨倒不出
itertools是python內(nèi)置的標(biāo)準(zhǔn)模塊,提供了很多簡(jiǎn)潔又高效的專用功能,使用得當(dāng)能夠極大的簡(jiǎn)化代碼行數(shù),同時(shí)所有方法都是實(shí)現(xiàn)了生成器函數(shù),這就意味著極大的節(jié)省內(nèi)存。
itertools提供的功能主要分為三大塊,以最新版本的3.10為例:
- 對(duì)可迭代對(duì)象無(wú)限迭代,無(wú)限輸出
- 對(duì)可迭代對(duì)象有限迭代
- 對(duì)可迭代對(duì)象排列組合
方法如下:

導(dǎo)入包
>>> from iteratortools import *
無(wú)限迭代
iteratortools.count(start=0, step=1)
數(shù)值生成器,可以指定起始位置和步長(zhǎng),并且步長(zhǎng)可以為浮點(diǎn)數(shù)。無(wú)限輸出,一直累加,在例子中需要邊睡眠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)
無(wú)限循環(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ù),否則將無(wú)限重復(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這種問(wèn)題又有一種新解法
有限迭代
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以及步長(zhǎng),新版本可以。
>>> 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ì)象的長(zhǎng)度未對(duì)齊,將根據(jù) fillvalue 填充缺失值。
迭代持續(xù)到耗光最長(zhǎng)的可迭代對(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 元素生成長(zhǎng)度為 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è)方法能夠完美解決算法中的全排列問(wèn)題,簡(jiǎn)直是量身定做。如果早知道這么簡(jiǎn)單,當(dāng)年考算法也不會(huì)。。,哎
可參見(jiàn)leetcode46題: 力扣

iteratortools.combinations(iteratorable, r)
返回由輸入 iteratorable 中元素組成長(zhǎng)度為 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ù)問(wèn)題

iteratortools.combinations_with_replacement(iteratorable, r)
返回由輸入 iteratorable 中元素組成的長(zhǎng)度為 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)庫(kù)itertools的文章就介紹到這了,更多相關(guān)Python 標(biāo)準(zhǔn)庫(kù)itertools內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python中itertools簡(jiǎn)介使用介紹
- python迭代器模塊itertools常用的方法
- Python中itertools模塊的使用教程詳解
- Python中itertools庫(kù)的四個(gè)函數(shù)介紹
- python中itertools模塊使用小結(jié)
- 關(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)文章
Python實(shí)現(xiàn)字符串格式化輸出的方法詳解
這篇文章主要介紹了Python實(shí)現(xiàn)字符串格式化輸出的方法,結(jié)合具體實(shí)例形式總結(jié)分析了Python字符串格式化輸出的各種常用操作技巧,需要的朋友可以參考下2017-09-09
PyTorch中view()與?reshape()的區(qū)別詳析
這篇文章主要給大家介紹了關(guān)于PyTorch中view()?與?reshape()?區(qū)別的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-01-01
Python實(shí)現(xiàn)合并多個(gè)Excel文件中的指定sheet
這篇文章主要為大家介紹了一個(gè)用于合并多個(gè)Excel文件中指定sheet的Python代碼,這個(gè)功能可以方便地整理和分析數(shù)據(jù),文中的示例代碼簡(jiǎn)潔易懂,需要的可以參考下2023-10-10
Python使用文件操作實(shí)現(xiàn)一個(gè)XX信息管理系統(tǒng)的示例
這篇文章主要介紹了Python使用文件操作實(shí)現(xiàn)一個(gè)XX信息管理系統(tǒng)的示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
一文帶你學(xué)會(huì)如何利用Python實(shí)現(xiàn)一個(gè)三維繪圖系統(tǒng)
tkinter是Python標(biāo)準(zhǔn)庫(kù)中自帶的GUI工具,使用十分方便,所以本文旨在帶大家學(xué)會(huì)如何將matplotlib嵌入到tkinter中并繪制三維繪圖系統(tǒng),感興趣的可以了解下2023-09-09

