欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python排列組合庫itertools的具體使用

 更新時間:2024年01月22日 08:56:28   作者:尊貴的架構(gòu)師  
排列組合是數(shù)學(xué)中必不可少的一部分, Python 提供了itertools庫,該庫具有計算排列和組合的內(nèi)置函數(shù),本文主要介紹了python排列組合庫itertools的具體使用,具有一定的參考價值,感興趣的可以了解下

 一、【說在前面】

看到這篇文章的大兄弟您們好,我們經(jīng)常說調(diào)包俠、CRUD仔,用來鄙視不會自己造輪子的工程師,不過筆者認(rèn)為python的精髓就是調(diào)庫。庫很快,自己寫大概率更慢。調(diào)包俠也是有高低貴賤之分的,今天介紹一個特別好用的庫——itertools

這個庫會自動產(chǎn)生一些諸如:排列、組合、笛卡爾積、多迭代器整合、zip等。更詳細(xì)可以看這個官網(wǎng)一手資料。

itertools — Functions creating iterators for efficient looping — Python 3.12.1 documentation

二、【正式介紹】 

(1)無限循環(huán)迭代器

count(start[, step]):

count() 函數(shù)生成一個從 start 開始的無限遞增的整數(shù)序列。如果提供了 step 參數(shù),則按照步長遞增。

Arguments:

start: 序列的起始值,默認(rèn)為 0。

step: 序列的步長,默認(rèn)為 1。

Results:

生成一個無限遞增的整數(shù)序列。

Example:

from itertools import count # 從10開始,步長為2,不寫默認(rèn)步長為1
for i in count(10, 2): 
    print(i)

# 10,12,14,16……

cycle(iterable):

cycle() 函數(shù)接受一個可迭代對象,并無限循環(huán)輸出該可迭代對象的元素。

Arguments:

iterable: 要循環(huán)的可迭代對象。

Results:

無限循環(huán)輸出可迭代對象的元素。

Example:

from itertools import cycle # 無限循環(huán)輸出 'A', 'B', 'C' 
for elem in cycle('ABC'): 
    print(elem)

repeat(elem [,n]):

repeat() 函數(shù)生成一個無限重復(fù)輸出某個元素的序列,或者最多重復(fù) n 次。不寫n則會一直重復(fù)

Arguments:

elem: 要重復(fù)的元素。

n: 最多重復(fù)的次數(shù),默認(rèn)為無限次。

Results:

生成一個無限重復(fù)輸出元素的序列,或者最多重復(fù) n 次。

Example:

from itertools import repeat # 重復(fù)輸出數(shù)字 10,最多重復(fù) 3 次 
for elem in repeat(10, 3): 
    print(elem)

(2)最短序列終止迭代器

1. accumulate(p [,func]):

將可迭代對象中的元素累積起來,可指定累積的函數(shù)。

Example:

from itertools import accumulate # 累積和 
result = accumulate([1, 2, 3, 4, 5]) 
print(list(result)) # 輸出 [1, 3(1+2), 6(1+2+3), 10, 15]

2. batched(p, n):

將可迭代對象劃分為大小為 n 的批次。

Example:

from itertools import batched # 劃分為大小為 3 的批次 
result = batched('ABCDEFG', n=3) 
print(list(result)) # 輸出 [('A', 'B', 'C'), ('D', 'E', 'F'), ('G',)]

3. chain(p, q, …):

將多個可迭代對象連接成一個單一的迭代器。

Example:

from itertools import chain # 連接兩個字符串 

result = chain('ABC', 'DEF') 
print(list(result)) # 輸出 ['A', 'B', 'C', 'D', 'E', 'F']

4. chain.from_iterable(iterable):

與 chain 類似,但接受一個可迭代對象的可迭代對象。

Example:

from itertools import chain # 連接兩個字符串列表 
result = chain.from_iterable(['ABC', 'DEF']) 
print(list(result)) # 輸出 ['A', 'B', 'C', 'D', 'E', 'F']

5. compress(data, selectors):

返回一個通過選擇器篩選的數(shù)據(jù)元素序列。

Example:

from itertools import compress # 通過選擇器篩選數(shù)據(jù) 
result = compress('ABCDEF', [1, 0, 1, 0, 1, 1]) # 1有0無
print(list(result)) # 輸出 ['A', 'C', 'E', 'F']

6. dropwhile(pred, seq):

返回在預(yù)測失敗之后的序列元素。

Example:

from itertools import dropwhile # 在預(yù)測失敗之后的序列元素 
result = dropwhile(lambda x: x < 5, [1, 4, 6, 4, 1])  # 當(dāng)遇到第一個不滿足條件(即大于等于 5)的元素時,停止迭代并返回余下的元素 [6, 4, 1]
print(list(result)) # 輸出 [6, 4, 1]

7. filterfalse(pred, seq):

返回使預(yù)測結(jié)果為假的序列元素。

Example:

from itertools import filterfalse # 使預(yù)測結(jié)果為假的序列元素 
result = filterfalse(lambda x: x % 2, range(10)) # 與2取模,奇數(shù)會留1所以不顯示

print(list(result)) # 輸出 [0, 2, 4, 6, 8]

8. groupby(iterable[, key]):

根據(jù)鍵函數(shù)對序列進(jìn)行分組。

筆者記得,力扣有一道壓縮算法,比如sss44444改為s3,45,就可以拿這個做,

Example:

from itertools import groupby # 根據(jù)首字母分組 
result = groupby('AAAABBBCCDAABBB') 
for key, group in result: 
    print(key, list(group)) # 輸出 # A ['A', 'A', 'A', 'A'] # B ['B', 'B', 'B'] # C ['C', 'C'] # D ['D'] # A ['A', 'A'] # B ['B', 'B', 'B']

9. islice(seq, [start,] stop [, step]):

返回切片后的序列元素。

Example:

from itertools import islice # 切片序列 seq[start:stop:step]
result = islice('ABCDEFG', 2, None)  # 從索引 = 2開始切,這個跟py自帶的切片一個道理
print(list(result)) # 輸出 ['C', 'D', 'E', 'F', 'G']

10. pairwise(iterable):

返回序列中兩兩相鄰的元素。

Example:

from itertools import pairwise # 兩兩相鄰的元素
result = pairwise('ABCDEFG') 
print(list(result)) 
# 輸出 [('A', 'B'), ('B', 'C'), ('C', 'D'), ('D', 'E'), ('E', 'F'), ('F', 'G')]

11. starmap(func, seq):

對可迭代對象中的元素應(yīng)用函數(shù),元素以參數(shù)元組的形式傳遞給函數(shù)。

Example:

from itertools import starmap # 應(yīng)用 pow 函數(shù) 
result = starmap(pow, [(2, 5), (3, 2), (10, 3)])  # 很好理解,本質(zhì)就是一個map操作
print(list(result)) # 輸出 [32, 9, 1000]

12. takewhile(pred, seq):

返回在預(yù)測失敗之前的序列元素立即為。

Example:

from itertools import takewhile # 在預(yù)測失敗之前的序列元素 
result = takewhile(lambda x: x < 5, [1, 4, 6, 4, 1]) 
print(list(result)) # 輸出 [1, 4]

13. tee(it, n):

將一個迭代器拆分成多個相同的迭代器。

Example:

from itertools import tee # 將一個迭代器拆分成兩個 iter1, iter2 = tee('ABC') print(list(iter1)) # 輸出 ['A', 'B', 'C'] 
print(list(iter2)) # 輸出 ['A', 'B', 'C']

14. zip_longest(p, q, …):

將多個迭代器中的元素逐個配對,以最長的迭代器為準(zhǔn),可以指定填充值。

Example:

from itertools import zip_longest # 將兩個迭代器中的元素逐個配對 
result = zip_longest('ABCD', 'xy', fillvalue='-') 
print(list(result)) # 輸出 [('A', 'x'), ('B', 'y'), ('C', '-'), ('D', '-')]

(3)排列、組合、笛卡爾積

1. product(p, q, …, repeat=1):

  • product 函數(shù)用于計算多個可迭代對象的笛卡爾積,生成所有可能的組合。它的參數(shù)可以是多個可迭代對象,如 p, q, ...,以及一個可選的 repeat 參數(shù),用于指定重復(fù)的次數(shù)。
  • 例如,如果 p 是 [1, 2],q 是 [3, 4],那么 product(p, q) 將生成 [(1, 3), (1, 4), (2, 3), (2, 4)]
# 示例 1: product
p = [1, 2]
q = [3, 4]
result_product = list(itertools.product(p, q))
print(result_product)  # 輸出:[(1, 3), (1, 4), (2, 3), (2, 4)]

2. permutations(iterable, r=None):

  • permutations 函數(shù)用于生成可迭代對象中長度為 r 的所有排列。它的參數(shù)包括一個可迭代對象 iterable 和一個可選的 r 參數(shù),用于指定生成的排列長度。
  • 例如,如果 iterable 是 [1, 2, 3],r 是 2,那么 permutations(iterable, r) 將生成 [(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)],這是長度為 2 的所有可能排列。
# 示例 2: permutations
iterable = [1, 2, 3]
r = 2
result_permutations = list(itertools.permutations(iterable, r))
print(result_permutations)  # 輸出:[(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)]

3. combinations(iterable, r):

  • combinations 函數(shù)用于生成可迭代對象中長度為 r 的所有組合,按照排序順序生成。它的參數(shù)包括一個可迭代對象 iterable 和一個 r 參數(shù),用于指定生成的組合長度。
  • 例如,如果 iterable 是 [1, 2, 3],r 是 2,那么 combinations(iterable, r) 將生成 [(1, 2), (1, 3), (2, 3)],這是長度為 2 的所有可能組合,按照排序順序生成。
# 示例 3: combinations
iterable = [1, 2, 3]
r = 2
result_combinations = list(itertools.combinations(iterable, r))
print(result_combinations)  # 輸出:[(1, 2), (1, 3), (2, 3)]

4. combinations_with_replacement(iterable, r)

  • combinations_with_replacement 函數(shù)與 combinations 類似,用于生成可迭代對象中長度為 r 的所有組合,但不要求元素不重復(fù),可以包含重復(fù)元素。
  • 例如,如果 iterable 是 [1, 2],r 是 2,那么 combinations_with_replacement(iterable, r) 將生成 [(1, 1), (1, 2), (2, 2)],這是長度為 2 的所有可能組合,包括重復(fù)元素。
# 示例 4: combinations_with_replacement
iterable = [1, 2]
r = 2
result_combinations_with_replacement = list(itertools.combinations_with_replacement(iterable, r))
print(result_combinations_with_replacement)  # 輸出:[(1, 1), (1, 2), (2, 2)]

到此這篇關(guān)于python排列組合庫itertools的具體使用的文章就介紹到這了,更多相關(guān)python排列組合庫itertools內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

最新評論