溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Python中的itertools模塊的用法

發(fā)布時間:2021-07-24 09:36:48 來源:億速云 閱讀:154 作者:chen 欄目:編程語言

本篇內(nèi)容主要講解“Python中的itertools模塊的用法”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“Python中的itertools模塊的用法”吧!

在Python中有一個功能強(qiáng)大的迭代工具包itertools,是Python自帶的標(biāo)準(zhǔn)工具包之一。

product

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

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

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

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

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

比如print(list(itertools.permutations('abc',3))),共有種情況。

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

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

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

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

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

combinations_with_replacement

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

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

我們來看一個簡單的例子

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

運行結(jié)果輸出 1 2 3 1 2 3……一直周而復(fù)始,永不停息。

到此,相信大家對“Python中的itertools模塊的用法”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI