python中的itertools的使用詳解
今天了解了下python中內(nèi)置模塊itertools的使用,熟悉下,看能不能以后少寫幾個for,嘿嘿😁。
1.無窮的迭代器
1.1 count(start,[step])
count()接受兩個參數(shù)
- start:循環(huán)開始的數(shù)字
- step:循環(huán)中的間隔
from itertools import count """ 無窮的迭代器 count() """ c = count(0, 2) v = next(c) while v < 10: v = next(c) print(v, end=',')
1.2 cycle()
cycle就是一while True,無限循環(huán)里面的數(shù)字。
""" 無窮迭代器 cycle() """ from itertools import cycle c = cycle('ABCD') for i in range(10): print(next(c), end=',')
1.3 repeat(elem,[n])
重復(fù)迭代elem,n次
""" 無窮迭代器 repeat() """ from itertools import repeat r = repeat(1, 3) for i in range(3): print(next(r), end=',')
2. 迭代器
2.1 accumulate(p,[func])
使用func的函數(shù)對迭代對象p進(jìn)行累積。
""" 迭代器 accumulate() """ from itertools import accumulate test_list = [i for i in range(1, 11)] for i in accumulate(test_list): # 默認(rèn)是operator.add print(i, end=',') print() for i in accumulate(test_list, lambda x, y: x * y): # operator.mul print(i, end=',')
2.2 chain()
chain()中可以放多個迭代對象,然后一一迭代出來。
""" 迭代器 chain() """ from itertools import chain ch = chain([1, 2, 3], {4: 4, 5: 5}, {6, 7, 8}, (9,), [10, [11, 12]]) for i in ch: print(i)
2.3 chain.from_iterable()
跟chain不同的地方在于:
- chain: 可以接受多個迭代對象
- chain.from_iterable():可以接受一個可以產(chǎn)生迭代對象的迭代器
""" 迭代器 chain.from_iterable() """ def gen_iterables(): for i in range(10): yield range(i) for i in chain.from_iterable(gen_iterables()): print(i)
2.4 compress(data,selectors)
這是就是看下這個就知道了s是selectors中的元素。
(d[0] if s[0]), (d[1] if s[1]), ...
""" 迭代器 compress """ from itertools import compress print(list(compress(['A', 'B', 'C', 'D'], [0, 1, 1, 1])))
2.5 dropwhile(pred,seq)
循環(huán)開始的條件是,直到遇到第一次不滿足pred條件的情況,才開始遍歷。
""" 迭代器 dropwhile() """ from itertools import dropwhile l = [1, 7, 6, 3, 8, 2, 10] print(list(dropwhile(lambda x: x < 3, l)))
2.6 groupby
這個感覺挺有意思的,有點(diǎn)像sql中的group_by。可以對字符串,列表等進(jìn)行分組。
返回鍵和,組里的內(nèi)容
from itertools import groupby # 對字符串進(jìn)行分組 for k, g in groupby('11111234567'): print(k, list(g)) d = {1: 1, 2: 2, 3: 2} # 按照字典value來進(jìn)行分組 for k, g in groupby(d, lambda x: d.get(x)): print(k, list(g))
2.7 islice
這個就是對迭代對象進(jìn)行切割,不支持負(fù)數(shù),有點(diǎn)像range(1,10,2)這種
from itertools import islice print(list(islice('ABCDEFG', 2,3, None)))
2.8 zip_longest
這個和zip很像,不同地方在于:
- zip結(jié)束取決于里面最短的迭代對象
- zip_longest結(jié)束取決于里面最長的迭代對象
from itertools import zip_longest for x,y in zip_longest([1,2,3],[1,2]): print(x,y) for x,y in zip([1,2,3],[1,2]): print(x,y)
排列組合迭代器
3.1 product
相當(dāng)于 嵌套的for
“”" 排列組合迭代器 product 嵌套的for “”" from itertools import product for i,j in product([1,2,3],[4,5]): print(i,j
3.2 permutations
全排列,比如輸出123的全部情況。(1,2,3),(1,3,2)…
from itertools import permutations print(list(permutations('123')))
3.3 combinations(p,r)
從p中找出所有長度為r的排列情況… 有順序
from itertools import combinations print(list(combinations([1,2,3],2)))
3.4 combinations_with_replacement()
從p中找出所有長度為r的排列情況,有順序,但包括自身就是會重復(fù)的意思。
- combinations_with_replacement(‘ABCD', 2)
- AA AB AC AD BB BC BD CC CD DD
了解是了解了,就是用的時(shí)候不知道能不能想起來…
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Pygame改編飛機(jī)大戰(zhàn)制作兔子接月餅游戲
一年中秋又快到了,今年加入了Python的學(xué)習(xí)行列,得益于Python的開發(fā)效率和易讀性,網(wǎng)上寫文章的次數(shù)多了起來,既然是中秋節(jié)那肯定要搞個應(yīng)景的游戲才行2022-09-09PyQt5+QtChart實(shí)現(xiàn)柱狀圖的繪制
QChart是一個QGraphicScene中可以顯示的QGraphicsWidget。本文將利用QtChart實(shí)現(xiàn)柱狀圖的繪制,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-12-12Python實(shí)現(xiàn)視頻轉(zhuǎn)換為音頻的方法詳解
這篇文章主要為大家詳細(xì)Python如何將視頻轉(zhuǎn)換為音頻并將音頻文件保存到特定文件夾下,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-02-02Python利用第三方模塊實(shí)現(xiàn)壓縮css文件
在?Python?中可以使用多種方法來壓縮?CSS?文件。其中一種流行的方法是使用?csscompressor?庫,這個庫可以幫助你壓縮?CSS?代碼,下文就來和大家詳細(xì)聊聊2023-01-01Python HTMLTestRunner可視化報(bào)告實(shí)現(xiàn)過程解析
這篇文章主要介紹了Python HTMLTestRunner可視化報(bào)告實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04Python中列表索引 A[ : 2 ]與A[ : ,&nb
這篇文章主要介紹了Python中列表索引 A[ : 2 ]與A[ : , 2]的區(qū)別說明,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-05-05pytorch下的unsqueeze和squeeze的用法說明
這篇文章主要介紹了pytorch下的unsqueeze和squeeze的用法說明,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02