自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

深入Python中的itertools模塊

開發(fā) 后端
在Python中有一個功能強大的迭代工具包itertools,是Python自帶的標(biāo)準(zhǔn)工具包之一。

[[350835]]

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

product

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

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

笛卡爾乘積是指在數(shù)學(xué)中,兩個集合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ù)學(xué)或者字符的全排列方式。

全排列,即產(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.  
  6. print(list(itertools.permutations('abc',3)))  
  7. # 輸出如下 
  8. ('a''b''c'
  9. ('a''c''b'
  10. ('b''a''c'
  11. ('b''c''a'
  12. ('c''a''b'
  13. ('c''b''a'
  14. [('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ù)目的元素不重復(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用于對列表中元素逐個累加

  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.  
  3. its=["a","b","c","d","e","f","g","h"
  4. selector=[True,False,1,0,3,False,-2,"y"
  5. for item in itertools.compress(its,selector): 
  6.     print (item) 
  7.      
  8. 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.  
  8.  
  9. 10 
  10. 11 
  11. 12 
  12. 13 
  13. 14 
  14. 15 
  15. 16 
  16. 17 
  17. 18 
  18. 19 
  19. 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. 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……一直周而復(fù)始,永不停息。 

 

責(zé)任編輯:姜華 來源: Python之王
相關(guān)推薦

2020-11-11 08:24:06

collection

2023-11-27 15:08:52

Python編程語言

2023-11-28 11:22:51

Pythonitertools庫工具

2024-03-25 08:57:49

模塊迭代對象迭代器

2020-09-18 07:52:46

Itertools庫Python語言

2025-01-08 17:20:00

pytho數(shù)據(jù)分組itertools

2021-09-28 14:40:03

Python內(nèi)置庫itertools

2020-11-05 08:56:19

Python

2023-08-15 11:24:42

人工智能AI

2023-05-24 10:24:56

代碼Python

2023-11-15 08:32:16

正則表達(dá)式Python

2010-02-03 10:12:53

Python模塊

2024-01-22 12:10:57

zoneinfo模塊解析

2020-11-12 08:52:16

Python

2018-05-28 09:20:10

Python迭代for循環(huán)

2016-08-31 15:50:50

PythonThreadLocal變量

2021-08-12 15:45:23

Pythonimport模塊

2021-04-14 06:19:29

PythonPillow圖片處理模塊

2023-12-22 08:38:02

Pythondatetimetime

2010-04-12 16:28:41

無線通信模塊
點贊
收藏

51CTO技術(shù)棧公眾號