日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
深入Python中的itertools模塊

在Python中有一個(gè)功能強(qiáng)大的迭代工具包itertools,是Python自帶的標(biāo)準(zhǔn)工具包之一。

創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),貴池企業(yè)網(wǎng)站建設(shè),貴池品牌網(wǎng)站建設(shè),網(wǎng)站定制,貴池網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷,網(wǎng)絡(luò)優(yōu)化,貴池網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。

product

由于itertools是內(nèi)置庫(kù),不需要任何安裝,直接import itertools即可。

product 用于求多個(gè)可迭代對(duì)象的笛卡爾積(Cartesian Product),它跟嵌套的 for 循環(huán)等價(jià).即:

笛卡爾乘積是指在數(shù)學(xué)中,兩個(gè)集合X和Y的笛卡爾積(Cartesian product),又稱直積,表示為X × Y。

product(A, B)和 ``((x,y) for x in A for y in B)`一樣.

 
 
 
  1. import itertools
  2. for item in itertools.product([1,2,3],[100,200]):
  3.     print(item)
  4.     
  5.     
  6. # 輸出如下
  7. (1, 100)
  8. (1, 200)
  9. (2, 100)
  10. (2, 200)
  11. (3, 100)
  12. (3, 200)

permutations

通俗地講,permutations就是返回可迭代對(duì)象的所有數(shù)學(xué)或者字符的全排列方式。

全排列,即產(chǎn)生指定數(shù)目的元素的所有排列(順序有關(guān)),也就是高中排列組合中的那個(gè)A。

permutations它接受一個(gè)集合對(duì)象,然后產(chǎn)生一個(gè)元組序列。

比如print(list(itertools.permutations('abc',3))),共有種情況。

 
 
 
  1. items = ['a','b','c']
  2. from itertools import permutations
  3. for i in permutations(items):
  4.     print(i) #排列組合
  5. print(list(itertools.permutations('abc',3))) 
  6. # 輸出如下
  7. ('a', 'b', 'c')
  8. ('a', 'c', 'b')
  9. ('b', 'a', 'c')
  10. ('b', 'c', 'a')
  11. ('c', 'a', 'b')
  12. ('c', 'b', 'a')
  13. [('a', 'b', 'c'), ('a', 'c', 'b'), ('b', 'a', 'c'), ('b', 'c', 'a'), ('c', 'a', 'b'), ('c', 'b', 'a')]

如果需要指定長(zhǎng)度的所有排列,可以傳遞一個(gè)可選的長(zhǎng)度參數(shù)r。

 
 
 
  1. items = ['a','b','c']
  2. from itertools import permutations
  3. for i in permutations(items,2):
  4.     print(i) #排列組合
  5.     
  6. # 輸出如下
  7. ('a', 'b')
  8. ('a', 'c')
  9. ('b', 'a')
  10. ('b', 'c')
  11. ('c', 'a')
  12. ('c', 'b')

combinations

求列表或生成器中指定數(shù)目的元素不重復(fù)的所有組合

itertools.permutations(iter,r) 和 itertools.combinations(iter,r)的區(qū)別是:前者是permutations允許重復(fù)使用,后者combinations是不能重復(fù)使用

 
 
 
  1. >>> print(list(itertools.combinations('abc',3)))
  2. [('a', 'b', 'c')]

combinations_with_replacement

combinations_with_replacement和combinations很相似,唯一的不同在于前者combinations_with_replacement集合類型中的數(shù)據(jù)是可以重復(fù)的

 
 
 
  1. >>> print(list(itertools.combinations_with_replacement('abc',3)))
  2. [('a', 'a', 'a'), ('a', 'a', 'b'), ('a', 'a', 'c'), ('a', 'b', 'b'), ('a', 'b', 'c'), ('a', 'c', 'c'), ('b', 'b', 'b'), ('b', 'b', 'c'), ('b', 'c', 'c'), ('c', 'c', 'c')]

accumulate

accumulate用于對(duì)列表中元素逐個(gè)累加

 
 
 
  1. >>> import itertools
  2. >>> x = itertools.accumulate(range(10))
  3. >>> print(list(x))
  4. [0, 1, 3, 6, 10, 15, 21, 28, 36, 45]

compress

compress()是篩選工具,它接受一個(gè)可迭代對(duì)象以及一個(gè)布爾選擇序列作為輸入,輸出時(shí)會(huì)將所有布爾序列中為True的可迭代對(duì)象輸出。

 
 
 
  1. import itertools
  2. its=["a","b","c","d","e","f","g","h"]
  3. selector=[True,False,1,0,3,False,-2,"y"]
  4. for item in itertools.compress(its,selector):
  5.     print (item)
  6.     
  7. a
  8. c
  9. e
  10. g
  11. h   

count

count(初值=0, 步長(zhǎng)=1)是 創(chuàng)建一個(gè)迭代器,從傳入的起始參數(shù)開始的均勻間隔的數(shù)值。

我們來(lái)看一個(gè)簡(jiǎn)單的例子

 
 
 
  1. from itertools import count
  2. for i in count(10): #從10開始無(wú)限循環(huán)
  3.     if i > 20: 
  4.         break
  5.     else:
  6.         print(i)
  7. 10
  8. 11
  9. 12
  10. 13
  11. 14
  12. 15
  13. 16
  14. 17
  15. 18
  16. 19
  17. 20

chain

chain鏈條,主要用來(lái)把多個(gè)序列連在一起做迭代。

 
 
 
  1. import itertools
  2. chain = itertools.chain([1, 2, 3], [4, 5, 6])
  3. for c in chain:
  4.    print(c)
  5. 1
  6. 2
  7. 3
  8. 4
  9. 5
  10. 6  

chain還有一個(gè)非常重要的功能就是展平列表。

 
 
 
  1. >>> list(itertools.chain([1, 2, 3], [4, 5], [6] ,[7,8]))
  2. [1, 2, 3, 4, 5, 6, 7, 8]

cycle

 
 
 
  1. import itertools
  2. cycle = itertools.cycle([1, 2, 3])
  3. for c in cycle:
  4.    print(c)

運(yùn)行結(jié)果輸出 1 2 3 1 2 3……一直周而復(fù)始,永不停息。


當(dāng)前標(biāo)題:深入Python中的itertools模塊
標(biāo)題路徑:http://m.5511xx.com/article/cccpdcj.html