成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

深入Python中的itertools模塊

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

[[350835]]

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

product

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

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

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

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

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

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

如果需要指定長度的所有排列,可以傳遞一個可選的長度參數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

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

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

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

combinations_with_replacement

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

  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)是 創建一個迭代器,從傳入的起始參數開始的均勻間隔的數值。

我們來看一個簡單的例子

  1. from itertools import count 
  2. for i in count(10): #從10開始無限循環 
  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) 

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

 

責任編輯:姜華 來源: Python之王
相關推薦

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數據分組itertools

2021-09-28 14:40:03

Python內置庫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

正則表達式Python

2020-11-12 08:52:16

Python

2018-05-28 09:20:10

Python迭代for循環

2016-08-31 15:50:50

PythonThreadLocal變量

2024-01-22 12:10:57

zoneinfo模塊解析

2010-02-03 10:12:53

Python模塊

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技術棧公眾號

主站蜘蛛池模板: 一区二区三区小视频 | 成人区精品一区二区婷婷 | 国产精品电影在线观看 | 在线国产一区二区 | 亚洲国产精品va在线看黑人 | 在线日韩欧美 | 国产精品波多野结衣 | 成人 在线 | 99热这里只有精品8 激情毛片 | 国产一级久久久久 | 欧美色欧美亚洲另类七区 | 欧美一区二区在线观看 | 中文字幕二区 | 久久久婷婷 | 手机av在线| 一区二区精品视频 | 欧美xxxx网站 | 国产精品色婷婷久久58 | 亚洲一区视频在线 | 久久在看 | 亚洲成人黄色 | 亚洲成av人影片在线观看 | 日韩伦理一区二区 | 青青久草| 久久男人| 欧美一级在线观看 | 久久久久久久久99 | 日韩欧美专区 | 亚洲视频在线看 | 一区中文字幕 | 成人av观看 | 国产精品久久九九 | 国产乱码精品1区2区3区 | 亚洲 欧美 日韩在线 | 国产传媒在线观看 | 国产欧美一区二区三区久久 | 亚洲国产欧美在线 | 国产一区电影 | 国产成人精品午夜视频免费 | 久久国产精品一区 | 91久操视频 |