淺析Python自帶性能強悍的標準庫itertools
前言
?
可迭代對象就像密閉容器里的水,有貨倒不出
itertools是python內(nèi)置的標準模塊,提供了很多簡潔又高效的專用功能,使用得當能夠極大的簡化代碼行數(shù),同時所有方法都是實現(xiàn)了生成器函數(shù),這就意味著極大的節(jié)省內(nèi)存。
itertools提供的功能主要分為三大塊,以最新版本的3.10為例:
- 對可迭代對象無限迭代,無限輸出
- 對可迭代對象有限迭代
- 對可迭代對象排列組合
方法如下:
導入包
>>> from iteratortools import *
無限迭代
iteratortools.count(start=0, step=1)
數(shù)值生成器,可以指定起始位置和步長,并且步長可以為浮點數(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)取出可迭代對象里的元素
>>> 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])
不斷重復輸出整個object,如果指定了重復次數(shù),則輸出指定次數(shù),否則將無限重復。
>>> 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
有了這個神器,對輸出10次hello world這種問題又有一種新解法
有限迭代
iteratortools.accumulate(iteratorable[, func, *, initial=None])
返回對列表中元素逐項的操作,操作有:
- 累加,返回累加到每一項的列表
- 累乘,返回累乘到每一項的列表
- 最小值,返回到當前項的最小值
- 最大值,返回到當前項的最大值
>>> [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òu)建成一個新的可迭代對象,統(tǒng)一返回。類似于將多個對象鏈成一條串
>>> iterator = chain([1,2,3],['a','b','c'],(5,6,7)) >>> list(iterator) [1, 2, 3, 'a', 'b', 'c', 5, 6, 7]
優(yōu)點:可以將多個可迭代對象整合成一個,避免逐個取值
chain.from_iteratorable(iteratorable)
將一個迭代對象中將所有元素類似于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)
按照條件篩選,丟棄掉第一次不符合條件時之前的所有元素
>>> 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條件篩選可迭代對象中的元素,只要元素為真就返回,第一次遇到不符合的條件就退出。
按照條件篩選,丟棄第一次遇到不符合條件之后的元素。行為類似于上一個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])
對迭代器進行切片,老版本中不能指定start和stop以及步長,新版本可以。
>>> iterator = count() >>> slice_iterator = islice(iterator, 10, 20, 2) >>> list(slice_iterator) [10, 12, 14, 16, 18]
iteratortools.starmap(function, iteratorable)
將function作用于可迭代對象上,類似于map函數(shù)
iteratortools.tee(iteratorable, n=2)
從一個可迭代對象中返回 n 個獨立的迭代器
>>> 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)建一個迭代器,從每個可迭代對象中收集元素。如果可迭代對象的長度未對齊,將根據(jù) fillvalue 填充缺失值。
迭代持續(xù)到耗光最長的可迭代對象。大致相當于:
>>> iterator = zip_longest("ABCD", "xy", fillvalue="-") >>> list(iterator) [('A', 'x'), ('B', 'y'), ('C', '-'), ('D', '-')]
排列組合迭代
iteratortools.product(*iteratorables, repeat=1)
生成多個可迭代對象的笛卡爾積
大致相當于生成器表達式中的嵌套循環(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è)定為要重復的次數(shù)。例如,product(A, repeat=4) 和 product(A, A, A, A) 是一樣的
iteratortools.permutations(iteratorable, r=None)
由 iteratorable 元素生成長度為 r 的排列。元素的排列,類似于給一個[1,2,3],選取其中兩個元素,一共有多少種組合方法?不要求元素排列之后的位置。
>>> 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)]
這個方法能夠完美解決算法中的全排列問題,簡直是量身定做。如果早知道這么簡單,當年考算法也不會。。,哎
可參見leetcode46題: 力扣
iteratortools.combinations(iteratorable, r)
返回由輸入 iteratorable 中元素組成長度為 r 的子序列。元素不可重復使用。子序列是要求元素在排列之后和之前的相對位置不變的。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) []
這個方法可以曲線解決組合總數(shù)問題
iteratortools.combinations_with_replacement(iteratorable, r)
返回由輸入 iteratorable 中元素組成的長度為 r 的子序列,允許每個元素可重復出現(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自帶性能強悍的標準庫itertools的文章就介紹到這了,更多相關(guān)Python 標準庫itertools內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python中itertools簡介使用介紹
- python迭代器模塊itertools常用的方法
- Python中itertools模塊的使用教程詳解
- Python中itertools庫的四個函數(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實現(xiàn)截取屏幕保存文件,刪除N天前截圖的例子
今天小編就為大家分享一篇python實現(xiàn)截取屏幕保存文件,刪除N天前截圖的例子,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08Django 使用easy_thumbnails壓縮上傳的圖片方法
今天小編就為大家分享一篇Django 使用easy_thumbnails壓縮上傳的圖片方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07淺談django url請求與數(shù)據(jù)庫連接池的共享問題
今天小編就為大家分享一篇淺談django url請求與數(shù)據(jù)庫連接池的共享問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08