python中itertools模塊使用小結(jié)
Python 內(nèi)置的 itertools 模塊包含了一系列用來產(chǎn)生不同類型迭代器的函數(shù)或類,這些函數(shù)的返回都是一個(gè)迭代器,我們可以通過 for 循環(huán)來遍歷取值,也可以使用 next() 來取值。
itertools 模塊提供的迭代器函數(shù)有以下幾種類型:
- 無限迭代器:生成一個(gè)無限序列,比如自然數(shù)序列 1, 2, 3, 4, ...;
- 有限迭代器:接收一個(gè)或多個(gè)序列(sequence)作為參數(shù),進(jìn)行組合、分組和過濾等;
- 組合生成器:序列的排列、組合,求序列的笛卡兒積等。
itertools
高效循環(huán)下創(chuàng)建循環(huán)器的標(biāo)準(zhǔn)庫
Infinite itertools,無限迭代器
itertools.count(start=0, step=10)
默認(rèn)返回一個(gè)從0開始,依次+10的自然數(shù)迭代器,如果你不停止,它會(huì)一直運(yùn)行下去。
可以用start指定開始的位置,step是步長。
import itertools
c = itertools.count(start=0, step=1)
for i in c:
print(i)
# 0
# 10
# 20
# 30
# 40
# 50
# ...
itertools.cycle(iterable)
傳入一個(gè)可迭代對(duì)象,然后無限循環(huán)迭代。
import itertools
# itertools.count()
l = [1,2,3,4,5]
c = itertools.cycle(l)
for i in c:
print(i)
# 1
# 2
# 3
# 4
# 5
# 1
# 2
# 3
# 4
# 5
# ...
itertools.repeat(p_object, times=None)
重復(fù)迭代一個(gè)對(duì)象p_object,如果不指定times,則會(huì)迭代無數(shù)次,也可以通過times參數(shù)指定迭代的次數(shù)。
import itertools
# itertools.count()
l = [1,2,3,4,5]
c = itertools.repeat(l, 5)
for i in c:
print(i)
# [1, 2, 3, 4, 5]
# [1, 2, 3, 4, 5]
# [1, 2, 3, 4, 5]
# [1, 2, 3, 4, 5]
# [1, 2, 3, 4, 5]
Iterators terminating on the shortest input sequence
itertools.accumulate(iterable, func)
返回序列的累計(jì)值或者其他二進(jìn)制函數(shù)。
import itertools
# itertools.count()
l = [1,2,3,4,5]
c = itertools.accumulate(l)
print(c)
for i in c:
print(i)
# 1
# 3
# 6
# 10
# 15
accumulate()仍然返回的是一個(gè)迭代器,傳一個(gè)list,在for循環(huán)中遍歷打印的時(shí)候發(fā)現(xiàn),它做了累加操作。(迭代第一個(gè)數(shù),就是前一個(gè)數(shù)的和,迭代到第二個(gè)數(shù)時(shí),就是前兩個(gè)數(shù)的和,以此類推)
并且做遞加操作時(shí)支持:list, tuple, str, set, dict
傳入的是dict對(duì)象,那么會(huì)累加迭代dict的key:
import itertools
# itertools.count()
d = {'a': 1, 'b': 2, 'c': 3}
c = itertools.accumulate(d)
print(c)
for i in c:
print(i)
# <itertools.accumulate object at 0x000001F7A0A90E48>
# a
# ab
# abc
itertools.chain(*iterables)
chain()方法中的參數(shù)可以傳入多個(gè)序列,而且只要是序列即可,不限定序列的數(shù)據(jù)類型。
如:迭代list, tuple, str三個(gè)序列
import itertools
l = [1, 2, 3, 4, 5]
t = (1, 2, 3, 4, 5)
s = 'abcdefg'
c = itertools.chain(l, t, s)
print(c)
for i in c:
print(i)
# <itertools.chain object at 0x0000026E64801448>
# 1
# 2
# 3
# 4
# 5
# 1
# 2
# 3
# 4
# 5
# a
# b
# c
# d
# e
# f
# g
itertools 笛卡爾積
import itertools
d = [
[{"a": 1}, {"b": 2}], [{"c": 3}, {"d": 4}, {"e": 5}], [{"f": 6}, {"g": 7}]
]
print(*d)
for i in itertools.product(*d):
print(i)
# [{'a': 1}, {'b': 2}] [{'c': 3}, {'d': 4}, {'e': 5}] [{'f': 6}, {'g': 7}]
# ({'a': 1}, {'c': 3}, {'f': 6})
# ({'a': 1}, {'c': 3}, {'g': 7})
# ({'a': 1}, {'d': 4}, {'f': 6})
# ({'a': 1}, {'d': 4}, {'g': 7})
# ({'a': 1}, {'e': 5}, {'f': 6})
# ({'a': 1}, {'e': 5}, {'g': 7})
# ({'b': 2}, {'c': 3}, {'f': 6})
# ({'b': 2}, {'c': 3}, {'g': 7})
# ({'b': 2}, {'d': 4}, {'f': 6})
# ({'b': 2}, {'d': 4}, {'g': 7})
# ({'b': 2}, {'e': 5}, {'f': 6})
# ({'b': 2}, {'e': 5}, {'g': 7})
到此這篇關(guān)于python中的itertools模塊簡單使用的文章就介紹到這了,更多相關(guān)python itertools模塊使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python中itertools簡介使用介紹
- python迭代器模塊itertools常用的方法
- Python中itertools模塊的使用教程詳解
- Python中itertools庫的四個(gè)函數(shù)介紹
- 淺析Python自帶性能強(qiáng)悍的標(biāo)準(zhǔn)庫itertools
- 關(guān)于Python 內(nèi)置庫 itertools
- Python函數(shù)式編程中itertools模塊詳解
- Python編程itertools模塊處理可迭代集合相關(guān)函數(shù)
- 關(guān)于Python中 循環(huán)器 itertools的介紹
- 詳解python itertools功能
- python排列組合庫itertools的具體使用
相關(guān)文章
python?泛型函數(shù)--singledispatch的使用解讀
這篇文章主要介紹了python?泛型函數(shù)--singledispatch的使用解讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09
python+opencv實(shí)現(xiàn)車道線檢測
這篇文章主要為大家詳細(xì)介紹了python+opencv實(shí)現(xiàn)車道線檢測,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-02-02
Python?十大經(jīng)典排序算法實(shí)現(xiàn)詳解
排序算法是《數(shù)據(jù)結(jié)構(gòu)與算法》中最基本的算法之一。排序算法可以分為內(nèi)部排序和外部排序,內(nèi)部排序是數(shù)據(jù)記錄在內(nèi)存中進(jìn)行排序,而外部排序是因排序的數(shù)據(jù)很大,一次不能容納全部的排序記錄,在排序過程中需要訪問外存2022-01-01
Python之tkinter面板PanedWindow的使用
這篇文章主要介紹了Python之tkinter面板PanedWindow的使用及說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05
Python機(jī)器學(xué)習(xí)庫sklearn(scikit-learn)的基礎(chǔ)知識(shí)和高級(jí)用法
Scikit-Learn是 Python 最流行的機(jī)器學(xué)習(xí)庫之一,它提供了各種工具來實(shí)現(xiàn)、評(píng)估和探索各種學(xué)習(xí)算法,用于,各種機(jī)器學(xué)習(xí)任務(wù),在本教程中,我們將介紹 Scikit-Learn 的基礎(chǔ)知識(shí)和一些高級(jí)用法,并提供一些實(shí)例代碼來幫助我們更好地理解2023-07-07
利用python的socket發(fā)送http(s)請求方法示例
這篇文章主要給大家介紹了關(guān)于利用python的socket發(fā)送http(s)請求的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用python具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧2018-05-05
Python實(shí)現(xiàn)Linux中的du命令
這篇文章主要介紹了Python實(shí)現(xiàn)Linux中簡單du命令,需要的朋友可以參考下2017-06-06

