首頁  >  文章  >  後端開發  >  深入認識Python中的itertools模組

深入認識Python中的itertools模組

coldplay.xixi
coldplay.xixi轉載
2020-11-12 17:32:171736瀏覽

Python影片教學欄位介紹itertools模組。

深入認識Python中的itertools模組

在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)`一樣.

import itertools
for item in itertools.product([1,2,3],[100,200]):
    print(item)
    
    
# 输出如下
(1, 100)
(1, 200)
(2, 100)
(2, 200)
(3, 100)
(3, 200)复制代码

permutations

通俗地講,permutations就是一個傳回可迭代物件的所有數學或字元的全排列方式。

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

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

例如print(list(itertools.permutations('abc',3))),共有A33=6A_3^3=6 種情況。

items = ['a','b','c']
from itertools import permutations
for i in permutations(items):
    print(i) #排列组合

print(list(itertools.permutations('abc',3))) 
# 输出如下
('a', 'b', 'c')
('a', 'c', 'b')
('b', 'a', 'c')
('b', 'c', 'a')
('c', 'a', 'b')
('c', 'b', 'a')
[('a', 'b', 'c'), ('a', 'c', 'b'), ('b', 'a', 'c'), ('b', 'c', 'a'), ('c', 'a', 'b'), ('c', 'b', 'a')]复制代码

如果需要指定長度的所有排列,可以傳遞一個可選的長度參數r

items = ['a','b','c']
from itertools import permutations
for i in permutations(items,2):
    print(i) #排列组合
    
# 输出如下
('a', 'b')
('a', 'c')
('b', 'a')
('b', 'c')
('c', 'a')
('c', 'b')复制代码

combinations

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

itertools.permutations(iter,r)itertools.combinations(iter,r)的差異是:前者是permutations允許重複使用,後者combinations是不能重複使用

>>> print(list(itertools.combinations('abc',3)))
[('a', 'b', 'c')]复制代码

combinations_with_replacement

combinations_with_replacementcombinations很相似,唯一的不同在於前者combinations_with_replacement集合類型中的資料是可以重複的

>>> print(list(itertools.combinations_with_replacement('abc',3)))
[('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用於對清單中元素逐一累加

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

compress

compress()是篩選工具,它接受一個可迭代物件以及一個布林選擇序列作為輸入,輸出時會將所有布林序列中為True的可迭代物件輸出。

import itertools

its=["a","b","c","d","e","f","g","h"]
selector=[True,False,1,0,3,False,-2,"y"]
for item in itertools.compress(its,selector):
    print (item)
    
a
c
e
g
h   
复制代码

count

count(初值=0, 步長=1)是建立一個迭代器,從傳入的起始參數開始的均勻間隔的數值。

我們來看一個簡單的例子

from itertools import count
for i in count(10): #从10开始无限循环
    if i > 20: 
        break
    else:
        print(i)


10
11
12
13
14
15
16
17
18
19
20复制代码

chain

chain鏈條,主要用來把多個序列連在一起做迭代。

import itertools
chain = itertools.chain([1, 2, 3], [4, 5, 6])
for c in chain:
   print(c)
1
2
3
4
5
6  
复制代码

chain還有一個很重要的功能就是展平清單。

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

cycle

import itertools
cycle = itertools.cycle([1, 2, 3])
for c in cycle:
   print(c)复制代码

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

相關免費學習推薦:python影片教學

以上是深入認識Python中的itertools模組的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:juejin.im。如有侵權,請聯絡admin@php.cn刪除