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

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

新聞中心

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

在Python中有一個功能強大的迭代工具包itertools,是Python自帶的標準工具包之一。

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

product

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

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

笛卡爾乘積是指在數(shù)學中,兩個集合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就是返回可迭代對象的所有數(shù)學或者字符的全排列方式。

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

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

比如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')]

如果需要指定長度的所有排列,可以傳遞一個可選的長度參數(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ù)目的元素不重復的所有組合

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

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

combinations_with_replacement

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

 
 
 
  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用于對列表中元素逐個累加

 
 
 
  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()是篩選工具,它接受一個可迭代對象以及一個布爾選擇序列作為輸入,輸出時會將所有布爾序列中為True的可迭代對象輸出。

 
 
 
  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, 步長=1)是 創(chuàng)建一個迭代器,從傳入的起始參數(shù)開始的均勻間隔的數(shù)值。

我們來看一個簡單的例子

 
 
 
  1. from itertools import count
  2. for i in count(10): #從10開始無限循環(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鏈條,主要用來把多個序列連在一起做迭代。

 
 
 
  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還有一個非常重要的功能就是展平列表。

 
 
 
  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)

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


新聞標題:深入Python中的itertools模塊
URL標題:http://www.5511xx.com/article/cccpdcj.html