Python中itertools模塊的使用教程詳解
itertools模塊的介紹
在Python中,迭代器(Iterator)是常用來做惰性序列的對象,只有當?shù)侥硞€值的時候,才會進行計算得出這個值。因此,迭代器可以用來存儲無限大的序列,這樣我們就不用把他一次性放在內(nèi)存中,而只在需要的時候進行計算。所以,對于讀取大文件或者無線集合,最好是使用迭代器。實際上,Python2的大多數(shù)函數(shù)都是返回列表等序列,而Python3都已經(jīng)改進為返回迭代器。
Python的內(nèi)置模塊itertools就是用來操作迭代器的一個模塊,包含的函數(shù)都是能夠創(chuàng)建迭代器來用于for循環(huán)或者next()。其中函數(shù)主要可以分為三類,分別是無限迭代器,有限迭代器,組合迭代器。
無限迭代器(Infinite Iterators)
這些函數(shù)可以生成無限的迭代器,我們主要學習以下三個函數(shù)的用法。
count([start=0, step=1]) 接收兩個可選整形參數(shù),第一個指定了迭代開始的值,第二個指定了迭代的步長。此外,start參數(shù)默認為0,step參數(shù)默認為1,可以根據(jù)需要來把這兩個指定為其它值,或者使用默認參數(shù)。
import itertools
for i in itertools.count(10,2):
print(i)
if i>20:
break
[Running] python -u "e:\pythonee\code\test.py"
10
12
14
16
18
20
22cycle(iterable) 是用一個可迭代對象中的元素來創(chuàng)建一個迭代器,并且復制自己的值,一直無限的重復下去。
import itertools
for i in itertools.cycle("abcd"):
print(i) # 具有無限的輸出,可以按ctrl+c來停止。
[Running] python -u "e:\pythonee\code\test.py"
a
b
c
d
a
b
c
d
a
brepeat(elem [,n])是將一個元素重復n遍或者無窮多遍,并返回一個迭代器。
import itertools
for i in itertools.repeat("abcd",5):
print(i)
[Running] python -u "e:\pythonee\code\test.py"
abcd
abcd
abcd
abcd
abcd組合迭代器(Combinatoric Iterators)
組合操作包括排列,笛卡兒積,或者一些離散元素的選擇,組合迭代器就是產(chǎn)生這樣序列的迭代器。我們來看看這幾個函數(shù)的用法。
product(*iterables, repeat=1) 得到的是可迭代對象的笛卡兒積,*iterables參數(shù)表示需要多個可迭代對象。這些可迭代對象之間的笛卡兒積,也可以使用for循環(huán)來實現(xiàn),例如 product(A, B) 與 ((x,y) for x in A for y in B)就實現(xiàn)一樣的功能。
import itertools
for i in itertools.product([1,2,3],[4,5,6]):
print(i)
[Running] python -u "e:\pythonee\code\test.py"
(1, 4)
(1, 5)
(1, 6)
(2, 4)
(2, 5)
(2, 6)
(3, 4)
(3, 5)
(3, 6)而 repeat 參數(shù)則表示這些可迭代序列重復的次數(shù)。例如 product(A, repeat=4) 與 product(A, A, A, A)實現(xiàn)的功能一樣。
import itertools
for i in itertools.product('ab','cd',repeat = 2):
print(i)
[Running] python -u "e:\pythonee\code\test.py"
('a', 'c', 'a', 'c')
('a', 'c', 'a', 'd')
('a', 'c', 'b', 'c')
('a', 'c', 'b', 'd')
('a', 'd', 'a', 'c')
('a', 'd', 'a', 'd')
('a', 'd', 'b', 'c')
('a', 'd', 'b', 'd')
('b', 'c', 'a', 'c')
('b', 'c', 'a', 'd')
('b', 'c', 'b', 'c')
('b', 'c', 'b', 'd')
('b', 'd', 'a', 'c')
('b', 'd', 'a', 'd')
('b', 'd', 'b', 'c')
('b', 'd', 'b', 'd')permutations(iterable,r=None)返回的是可迭代元素中的一個排列組合,并且是按順序返回的,且不包含重復的結(jié)果。
import itertools
for i in itertools.permutations('abc'):
print(i)
[Running] python -u "e:\pythonee\code\test.py"
('a', 'b', 'c')
('a', 'c', 'b')
('b', 'a', 'c')
('b', 'c', 'a')
('c', 'a', 'b')
('c', 'b', 'a')當然,第 2 個參數(shù)默認為None,它表示的是返回元組(tuple) 的長度,我們來嘗試一下傳入第二個參數(shù)。
import itertools
for i in itertools.permutations('abc',2):
print(i)
[Running] python -u "e:\pythonee\code\test.py"
('a', 'b')
('a', 'c')
('b', 'a')
('b', 'c')
('c', 'a')
('c', 'b')combinations(iterable,r) 返回的是可迭代對象所有的長度為 r 的子序列,注意這與前一個函數(shù) permutation 不同,permutation 返回的是排列,而 combinations 返回的是組合。
import itertools
for i in itertools.combinations('1234',2):
print(i)
[Running] python -u "e:\pythonee\code\test.py"
('1', '2')
('1', '3')
('1', '4')
('2', '3')
('2', '4')
('3', '4')combinations_with_replacement(iterable, r) 返回一個可與自身重復的元素組合,用法類似于 combinations 。
import itertools
for i in itertools.combinations_with_replacement('1234',2):
print(i)
[Running] python -u "e:\pythonee\code\test.py"
('1', '1')
('1', '2')
('1', '3')
('1', '4')
('2', '2')
('2', '3')
('2', '4')
('3', '3')
('3', '4')
('4', '4')有限迭代器(Iterators Terminating on the Shortest Input Sequence)
這里的函數(shù)有十來個,主要為大家介紹其中幾個常用的函數(shù)。
chain(*iterables) 可以把多個可迭代對象組合起來,形成一個更大的迭代器。
import itertools
for i in itertools.chain('good','bye'):
print(i)
[Running] python -u "e:\pythonee\code\test.py"
g
o
o
d
b
y
egroupby(iterable,key=None) 可以把相鄰元素按照 key 函數(shù)分組,并返回相應的 key 和 groupby,如果key函數(shù)為 None,則只有相同的元素才能放在一組。
import itertools
for key, group in itertools.groupby('AaaBBbcCAAa', lambda c: c.upper()):
print(list(group))
[Running] python -u "e:\pythonee\code\test.py"
['A', 'a', 'a']
['B', 'B', 'b']
['c', 'C']
['A', 'A', 'a']accumulate(iterable [,func]) 可以計算出一個迭代器,這個迭代器是由特定的二元函數(shù)的累計結(jié)果生成的,如果不指定的話,默認函數(shù)為求和函數(shù)。
import itertools
for i in itertools.accumulate([0,1,0,1,1,2,3,5]):
print(i)
[Running] python -u "e:\pythonee\code\test.py"
0
1
1
2
3
5
8
13如果我們指定這個累計函數(shù),則還能有不同的用法,例如,指定一個最大值函數(shù),或者自己定義的函數(shù)。
import itertools
for i in itertools.accumulate([2,1,4,3,5],max):
print(i)
[Running] python -u "e:\pythonee\code\test.py"
2
2
4
4
5到此這篇關(guān)于Python中itertools模塊的使用教程詳解的文章就介紹到這了,更多相關(guān)Python itertools模塊內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Django csrf 兩種方法設(shè)置form的實例
今天小編就為大家分享一篇Django csrf 兩種方法設(shè)置form的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-02-02
淺談python中常用的8種經(jīng)典數(shù)據(jù)結(jié)構(gòu)
這篇文章主要介紹了python中常用的8種經(jīng)典數(shù)據(jù)結(jié)構(gòu),包括原生數(shù)據(jù)結(jié)構(gòu),NumPy包中的數(shù)據(jù)結(jié)構(gòu),以及Pandas包中的數(shù)據(jù)結(jié)構(gòu),需要的朋友可以參考下2023-03-03
python 百度aip實現(xiàn)文字識別的實現(xiàn)示例
百度aip將圖片或掃描件中的文字識別成可編輯的文本,本文主要介紹了python 百度aip實現(xiàn)文字識別,具有一定的參考價值,感興趣的可以了解一下2021-08-08

