Python標(biāo)準(zhǔn)庫(kù)itertools的使用方法
Python標(biāo)準(zhǔn)庫(kù)itertools模塊介紹
itertools是python內(nèi)置的模塊,使用簡(jiǎn)單且功能強(qiáng)大,這里嘗試匯總整理下,并提供簡(jiǎn)單應(yīng)用示例;如果還不能滿足你的要求,歡迎加入補(bǔ)充。
使用Python標(biāo)準(zhǔn)庫(kù)itertools只需簡(jiǎn)單一句導(dǎo)入:import itertools
chain()
與其名稱意義一樣,給它一個(gè)列表如 lists/tuples/iterables,鏈接在一起;返回iterables對(duì)象。
letters = ['a', 'b', 'c', 'd', 'e', 'f']
booleans = [1, 0, 1, 0, 0, 1]
print(list(itertools.chain(letters,booleans)))
#輸出:['a', 'b', 'c', 'd', 'e', 'f', 1, 0, 1, 0, 0, 1]
print(tuple(itertools.chain(letters,letters[3:])))
#輸出('a', 'b', 'c', 'd', 'e', 'f', 'd', 'e', 'f')
print(set(itertools.chain(letters,letters[3:])))
#輸出:{'a', 'd', 'b', 'e', 'c', 'f'}
print(list(itertools.chain(letters,letters[3:])))
#輸出:['a', 'b', 'c', 'd', 'e', 'f', 'd', 'e', 'f']
for item in list(itertools.chain(letters,booleans)):
print(item)
count()
生成無界限序列,count(start=0, step=1) ,示例從100開始,步長(zhǎng)為2,循環(huán)10,打印對(duì)應(yīng)值;必須手動(dòng)break,count()會(huì)一直循環(huán)。
i = 0
for item in itertools.count(100,2):
i += 1
if i > 10 : break
print(item)
filterfalse()
Python filterfalse(contintion,data) 迭代過濾條件為false的數(shù)據(jù)。如果條件為空,返回data中為false的項(xiàng);
booleans = [1, 0, 1, 0, 0, 1] numbers = [23, 20, 44, 32, 7, 12] print(list(itertools.filterfalse(None,booleans))) #輸出:[0, 0, 0] print(list(itertools.filterfalse(lambda x : x < 20,numbers))) #輸出:[23, 20, 44, 32]
compress()
返回我們需要使用的元素,根據(jù)b集合中元素真值,返回a集中對(duì)應(yīng)的元素。
print(list(itertools.compress(letters,booleans))) # ['a', 'c', 'f']
starmap()
針對(duì)list中的每一項(xiàng),調(diào)用函數(shù)功能。starmap(func,list[]) ;
starmap(pow, [(2,5), (3,2), (10,3)]) --> 32 9 1000 >>> from itertools import * >>> x = starmap(max,[[5,14,5],[2,34,6],[3,5,2]]) >>> for i in x: >>> print (i) 14 34 5
repeat()
repeat(object[, times]) 重復(fù)times次;
repeat(10, 3) --> 10 10 10
dropwhile()
dropwhile(func, seq );當(dāng)函數(shù)f執(zhí)行返回假時(shí), 開始迭代序列
dropwhile(lambda x: x<5, [1,4,6,4,1]) --> 6 4 1
takewhile()
takewhile(predicate, iterable);返回序列,當(dāng)predicate為true是截止。
takewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4
islice()
islice(seq[, start], stop[, step]);返回序列seq的從start開始到stop結(jié)束的步長(zhǎng)為step的元素的迭代器
for i in islice("abcdef", 0, 4, 2):#a, c
print i
product()
product(iter1,iter2, ... iterN, [repeat=1]);創(chuàng)建一個(gè)迭代器,生成表示item1,item2等中的項(xiàng)目的笛卡爾積的元組,repeat是一個(gè)關(guān)鍵字參數(shù),指定重復(fù)生成序列的次數(shù)
# product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
# product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
for i in product([1, 2, 3], [4, 5], [6, 7]):
print i
(1, 4, 6)
(1, 4, 7)
(1, 5, 6)
(1, 5, 7)
(2, 4, 6)
(2, 4, 7)
(2, 5, 6)
(2, 5, 7)
(3, 4, 6)
(3, 4, 7)
(3, 5, 6)
(3, 5, 7)
permutations()
permutations(p[,r]);返回p中任意取r個(gè)元素做排列的元組的迭代器
for i in permutations([1, 2, 3], 3): print i (1, 2, 3) (1, 3, 2) (2, 1, 3) (2, 3, 1) (3, 1, 2) (3, 2, 1)
combinations()
combinations(iterable,r);創(chuàng)建一個(gè)迭代器,返回iterable中所有長(zhǎng)度為r的子序列,返回的子序列中的項(xiàng)按輸入iterable中的順序排序
note:不帶重復(fù)
for i in combinations([1, 2, 3], 2): print i (1, 2) (1, 3) (2, 3)
combinations_with_replacement()
同上, 帶重復(fù) 例子:
for i in combinations_with_replacement([1, 2, 3], 2): print i (1, 1) (1, 2) (1, 3) (2, 2) (2, 3) (3, 3)
應(yīng)用示例
求質(zhì)數(shù)序列中1,3,5,7,9,11,13,15三個(gè)數(shù)之和為35的三個(gè)數(shù);
def get_three_data(data_list,amount):
for data in list(itertools.combinations(data_list, 3)):
if sum(data) == amount:
print(data)
#(7, 13, 15)
更多python標(biāo)準(zhǔn)庫(kù)使用方法請(qǐng)點(diǎn)擊下面的相關(guān)文章
相關(guān)文章
python動(dòng)態(tài)加載包的方法小結(jié)
這篇文章主要介紹了python動(dòng)態(tài)加載包的方法,結(jié)合實(shí)例形式總結(jié)分析了Python動(dòng)態(tài)加載模塊,動(dòng)態(tài)增加屬性及動(dòng)態(tài)加載包的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-04-04
Python代碼使用 Pyftpdlib實(shí)現(xiàn)FTP服務(wù)器功能
FTP 服務(wù)器,在此之前我都是使用Linux的vsftpd軟件包來搭建FTP服務(wù)器的,現(xiàn)在發(fā)現(xiàn)了利用pyftpdlib可以更加簡(jiǎn)單的方法即可實(shí)現(xiàn)FTP服務(wù)器的功能 ,需要的朋友可以參考下2019-07-07
解讀調(diào)用jupyter?notebook文件內(nèi)的函數(shù)一種簡(jiǎn)單方法
這篇文章主要介紹了解讀調(diào)用jupyter?notebook文件內(nèi)的函數(shù)一種簡(jiǎn)單方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01
詳解用Python把PDF轉(zhuǎn)為Word方法總結(jié)
這篇文章主要介紹了詳解用Python把PDF轉(zhuǎn)為Word方法總結(jié),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
Python&Matlab實(shí)現(xiàn)灰狼優(yōu)化算法的示例代碼
灰狼優(yōu)化算法是一種群智能優(yōu)化算法,它的獨(dú)特之處在于一小部分擁有絕對(duì)話語權(quán)的灰狼帶領(lǐng)一群灰狼向獵物前進(jìn)。本文具體介紹了灰狼優(yōu)化算法的兩種實(shí)現(xiàn)示例代碼,需要的可以參考一下2022-03-03
python在windows和linux下獲得本機(jī)本地ip地址方法小結(jié)
這篇文章主要介紹了python在windows和linux下獲得本機(jī)本地ip地址方法,實(shí)例分析了Python獲得IP地址的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03
python使用yield壓平嵌套字典的超簡(jiǎn)單方法
這篇文章主要給大家介紹了關(guān)于python使用yield壓平嵌套字典的超簡(jiǎn)單方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用python具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11

