首頁  >  文章  >  後端開發  >  Python中itertools模組如何使用

Python中itertools模組如何使用

王林
王林轉載
2023-05-28 15:41:581431瀏覽

itertools — 為高效循環而建立迭代器的函數

accumulate(iterable: Iterable, func: None, initial:None)

iterable:需要操作的可迭代物件

func:可迭代物件需要操作的函數,必須包含兩個參數

initial: 累加的開始值

當使用func對可迭代物件進行雙目運算時,需要提供兩個參數。回傳的是迭代器,與這個方法類似的就是functools下的reduce,reduce和accumulate都是累計進行操作,不同的是reduce只會傳回最後的元素,而accumulate會顯示所有的元素,包含中間的元素,比較如下:

##初始值
區別 reduce #accumulate
傳回的是一個元素 傳回的是一個迭代器(包含中間處理的元素)
所屬模組 functools itertools
效能 略差 比reduce好一點
###可以設定初始值######可以設定初始值#############
import time
from itertools import accumulate
from functools import reduce

l_data = [1, 2, 3, 4]
data = accumulate(l_data, lambda x, y: x + y, initial=2)
print(list(data))
start = time.time()
for i in range(100000):
    data = accumulate(l_data, lambda x, y: x + y, initial=2)
print(time.time() - start)
start = time.time()
for i in range(100000):
    data = reduce(lambda x, y: x + y, l_data)
print(time.time() - start)
#输出
[2, 3, 5, 8, 12]
0.027924537658691406
0.03989362716674805

由上述結果可知,accumulate比reduce性能稍好一些,而且還能輸出中間的處理過程。

chain(*iterables)

iterables:接收多個可迭代物件

依序傳回多個迭代物件的元素,傳回的是一個迭代器,對於字典輸出元素時,預設會輸出字典的key

from itertools import chain
import time

list_data = [1, 2, 3]
dict_data = {"a": 1, "b": 2}
set_data = {4, 5, 6}
print(list(chain(list_data, dict_data, set_data)))

list_data = [1, 2, 3]
list_data2 = [4, 5, 6]

start = time.time()
for i in range(100000):
    chain(list_data, list_data2)
print(time.time() - start)

start = time.time()
for i in range(100000):
    list_data.extend(list_data2)
print(time.time() - start)
#输出
[1, 2, 3, 'a', 'b', 4, 5, 6]
0.012955427169799805
0.013965129852294922

combinations(iterable: Iterable, r)

iterable:需要操作的可迭代物件

r: 抽取的子序列元素的個數

操作可迭代對象,根據所需抽取的子序列個數返回子序列,子序列中的元素也是有序、不可重複並且是以元組的形式呈現的。

from itertools import combinations


data = range(5)
print(tuple(combinations(data, 2)))
str_data = "asdfgh"
print(tuple(combinations(str_data, 2)))
#输出
((0, 1), (0, 2), (0, 3), (0, 4), (1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4))
(('a', 's'), ('a', 'd'), ('a', 'f'), ('a', 'g'), ('a', 'h'), ('s', 'd'), ('s', 'f'), ('s', 'g'), ('s', 'h'), ('d', 'f'), ('d', 'g'), ('d', 'h'), ('f', 'g'), ('f', 'h'), ('g', 'h'))

combinations_with_replacement(iterable: Iterable, r)

與上述的combinations(iterable: Iterable, r)類似,不過差別在於,combinations_with_replacement的子序列的元素可以重複,也是有序的,具體如下:

from itertools import combinations_with_replacement


data = range(5)
print(tuple(combinations_with_replacement(data, 2)))
str_data = "asdfgh"
print(tuple(combinations_with_replacement(str_data, 2)))
#输出
((0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (1, 1), (1, 2), (1, 3), (1, 4), (2, 2), (2, 3), (2, 4), (3, 3), (3, 4), (4, 4))
(('a', 'a'), ('a', 's'), ('a', 'd'), ('a', 'f'), ('a', 'g'), ('a', 'h'), ('s', 's'), ('s', 'd'), ('s', 'f'), ('s', 'g'), ('s', 'h'), ('d', 'd'), ('d', 'f'), ('d', 'g'), ('d', 'h'), ('f', 'f'), ('f', 'g'), ('f', 'h'), ('g', 'g'), ('g', 'h'), ('h', 'h'))

compress(data: Iterable, selectors: Iterable)

data:需要操作的可迭代物件

selectors:判斷真值的可迭代對象,不能時str,最好是列表、元組、之類的

根據selectors中的元素是否為true來輸出data中對應索引的元素,以最短的為準,返回一個迭代器。

from itertools import compress


data = "asdfg"
list_data = [1, 0, 0, 0, 1, 4]
print(list(compress(data, list_data)))
#输出
['a', 'g']

count(start, step)

start: 開始的元素

step: 自開始元素成長的步長

產生一個遞增的迭代器,其起點為start,遞增步長為給定值,不會立即產生所有元素,建議使用next()方法進行元素的遞迴獲取。

from itertools import count


c = count(start=10, step=20)
print(next(c))
print(next(c))
print(next(c))
print(next(c))
print(c)
#输出
10
30
50
70
count(90, 20)

cycle(iterable)

iterable: 需要循環輸出的可迭代物件

回傳一個迭代器,循環輸出可迭代物件的元素。於count一樣,最好不要將結果轉換為可迭代對象,因為是循環,所以建議使用next()或for循環來取得元素。

from itertools import cycle

a = "asdfg"
data = cycle(a)
print(next(data))
print(next(data))
print(next(data))
print(next(data))
#输出
a
s
d
f

dropwhile(predicate, iterable)

predicate:是否捨棄元素的標準

iterable: 可迭代物件

#透過對predicate的計算結果進行篩選,回傳一個迭代器,在這個迭代器中捨棄那些計算結果為True的元素。無論後面的元素是True或False,在predicate為False時都會輸出。

from itertools import dropwhile


list_data = [1, 2, 3, 4, 5]
print(list(dropwhile(lambda i: i < 3, list_data)))
print(list(dropwhile(lambda x: x < 5, [1, 4, 6, 4, 1])))
#输出
[3, 4, 5]
[6, 4, 1]

filterfalse(predicate, iterable)

predicate:是否捨棄元素的標準

iterable: 可迭代物件

#產生一個迭代器,在對在每個元素執行操作前,判斷其是否滿足predicate條件。類似於filter方法,但是是filter的相反的.

import time
from itertools import filterfalse

print(list(filterfalse(lambda i: i % 2 == 0, range(10))))

start = time.time()
for i in range(100000):
    filterfalse(lambda i: i % 2 == 0, range(10))
print(time.time() - start)

start = time.time()
for i in range(100000):
    filter(lambda i: i % 2 == 0, range(10))
print(time.time() - start)
#输出
[1, 3, 5, 7, 9]
0.276653528213501
0.2768676280975342

由上述結果看出,filterfalse與filter性能相差不大

groupby(iterable, key=None)

iterable: 可迭代物件

key: 可選,需要對元素進行判斷的條件, 預設為x == x。

傳回一個迭代器,根據key返回連續的鍵和群組(連續符合key條件的元素)。

注意使用groupby進行分組前需要對其進行排序。

from itertools import groupby


str_data = "babada"
for k, v in groupby(str_data):
    print(k, list(v))


str_data = "aaabbbcd"
for k, v in groupby(str_data):
    print(k, list(v))


def func(x: str):
    print(x)
    return x.isdigit()


str_data = "12a34d5"
for k, v in groupby(str_data, key=func):
    print(k, list(v))
#输出
b [&#39;b&#39;]
a [&#39;a&#39;]
b [&#39;b&#39;]
a [&#39;a&#39;]
d [&#39;d&#39;]
a [&#39;a&#39;]
a [&#39;a&#39;, &#39;a&#39;, &#39;a&#39;]
b [&#39;b&#39;, &#39;b&#39;, &#39;b&#39;]
c [&#39;c&#39;]
d [&#39;d&#39;]
1
2
a
True [&#39;1&#39;, &#39;2&#39;]
3
False [&#39;a&#39;]
4
d
True [&#39;3&#39;, &#39;4&#39;]
5
False [&#39;d&#39;]
True [&#39;5&#39;]

islice(iterable, stop)\islice(iterable, start, stop[, step])

iterable: 需要操作的可迭代物件

start: 開始操作的索引位置

stop: 結束操作的索引位置

step: 步長

傳回一個迭代器。類似於切片,但是其索引不支援負數。

from itertools import islice
import time

list_data = [1, 5, 4, 2, 7]
#学习中遇到问题没人解答?小编创建了一个Python学习交流群:725638078

start = time.time()
for i in range(100000):
    data = list_data[:2:]

print(time.time() - start)
start = time.time()
for i in range(100000):
    data = islice(list_data, 2)
print(time.time() - start)
print(list(islice(list_data, 1, 3)))
print(list(islice(list_data, 1, 4, 2)))
#输出
0.010963201522827148
0.01595783233642578
[5, 4]
[5, 2]

0.010963201522827148
0.01595783233642578
[5, 4]
[5, 2]

由上述結果可以看出,切片表現比islice性能稍好一些。

pairwise(iterable)

需要操作的可迭代物件

傳回一個迭代器, 傳回可迭代物件中的連續重疊對,少於兩個回傳空。

from itertools import pairwise

str_data = "asdfweffva"
list_data = [1, 2, 5, 76, 8]
print(list(pairwise(str_data)))
print(list(pairwise(list_data)))
#输出
[(&#39;a&#39;, &#39;s&#39;), (&#39;s&#39;, &#39;d&#39;), (&#39;d&#39;, &#39;f&#39;), (&#39;f&#39;, &#39;w&#39;), (&#39;w&#39;, &#39;e&#39;), (&#39;e&#39;, &#39;f&#39;), (&#39;f&#39;, &#39;f&#39;), (&#39;f&#39;, &#39;v&#39;), (&#39;v&#39;, &#39;a&#39;)]
[(1, 2), (2, 5), (5, 76), (76, 8)]

permutations(iterable, r=None)

iterable: 需要操作的可迭代物件

r: 抽取的子序列

#與combinations類似,都是抽取可迭代物件的子序列,不過,permutations是不可重複,無序的, 與combinations_with_replacement剛好相反。

from itertools import permutations


data = range(5)
print(tuple(permutations(data, 2)))
str_data = "asdfgh"
print(tuple(permutations(str_data, 2)))
#输出
((0, 1), (0, 2), (0, 3), (0, 4), (1, 0), (1, 2), (1, 3), (1, 4), (2, 0), (2, 1), (2, 3), (2, 4), (3, 0), (3, 1), (3, 2), (3, 4), (4, 0), (4, 1), (4, 2), (4, 3))
((&#39;a&#39;, &#39;s&#39;), (&#39;a&#39;, &#39;d&#39;), (&#39;a&#39;, &#39;f&#39;), (&#39;a&#39;, &#39;g&#39;), (&#39;a&#39;, &#39;h&#39;), (&#39;s&#39;, &#39;a&#39;), (&#39;s&#39;, &#39;d&#39;), (&#39;s&#39;, &#39;f&#39;), (&#39;s&#39;, &#39;g&#39;), (&#39;s&#39;, &#39;h&#39;), (&#39;d&#39;, &#39;a&#39;), (&#39;d&#39;, &#39;s&#39;), (&#39;d&#39;, &#39;f&#39;), (&#39;d&#39;, &#39;g&#39;), (&#39;d&#39;, &#39;h&#39;), (&#39;f&#39;, &#39;a&#39;), (&#39;f&#39;, &#39;s&#39;), (&#39;f&#39;, &#39;d&#39;), (&#39;f&#39;, &#39;g&#39;), (&#39;f&#39;, &#39;h&#39;), (&#39;g&#39;, &#39;a&#39;), (&#39;g&#39;, &#39;s&#39;), (&#39;g&#39;, &#39;d&#39;), (&#39;g&#39;, &#39;f&#39;), (&#39;g&#39;, &#39;h&#39;), (&#39;h&#39;, &#39;a&#39;), (&#39;h&#39;, &#39;s&#39;), (&#39;h&#39;, &#39;d&#39;), (&#39;h&#39;, &#39;f&#39;), (&#39;h&#39;, &#39;g&#39;))

product(*iterables, repeat=1)

iterables: 可迭代對象,可以為多個

repeat: 可迭代對象的重複次數,也就是複製的次數

返回迭代器。類比排列組合,產生笛卡爾積的可迭代物件。 Product function is similar to zip function, but while zip matches elements one-to-one, product creates a one-to-many relationship.。

from itertools import product


list_data = [1, 2, 3]
list_data2 = [4, 5, 6]
print(list(product(list_data, list_data2)))
print(list(zip(list_data, list_data2)))

# 如下两个含义是一样的,都是将可迭代对象复制一份, 很方便的进行同列表的操作
print(list(product(list_data, repeat=2)))
print(list(product(list_data, list_data)))
# 同上述含义
print(list(product(list_data, list_data2, repeat=2)))
print(list(product(list_data, list_data2, list_data, list_data2)))
#输出
[(1, 4), (1, 5), (1, 6), (2, 4), (2, 5), (2, 6), (3, 4), (3, 5), (3, 6)]
[(1, 4), (2, 5), (3, 6)]
[(1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3)]
[(1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3)]
[(1, 4, 1, 4), (1, 4, 1, 5), (1, 4, 1, 6), (1, 4, 2, 4), (1, 4, 2, 5), (1, 4, 2, 6), (1, 4, 3, 4), (1, 4, 3, 5), (1, 4, 3, 6), (1, 5, 1, 4), (1, 5, 1, 5), (1, 5, 1, 6), (1, 5, 2, 4), (1, 5, 2, 5), (1, 5, 2, 6), (1, 5, 3, 4), (1, 5, 3, 5), (1, 5, 3, 6), (1, 6, 1, 4), (1, 6, 1, 5), (1, 6, 1, 6), (1, 6, 2, 4), (1, 6, 2, 5), (1, 6, 2, 6), (1, 6, 3, 4), (1, 6, 3, 5), (1, 6, 3, 6), (2, 4, 1, 4), (2, 4, 1, 5), (2, 4, 1, 6), (2, 4, 2, 4), (2, 4, 2, 5), (2, 4, 2, 6), (2, 4, 3, 4), (2, 4, 3, 5), (2, 4, 3, 6), (2, 5, 1, 4), (2, 5, 1, 5), (2, 5, 1, 6), (2, 5, 2, 4), (2, 5, 2, 5), (2, 5, 2, 6), (2, 5, 3, 4), (2, 5, 3, 5), (2, 5, 3, 6), (2, 6, 1, 4), (2, 6, 1, 5), (2, 6, 1, 6), (2, 6, 2, 4), (2, 6, 2, 5), (2, 6, 2, 6), (2, 6, 3, 4), (2, 6, 3, 5), (2, 6, 3, 6), (3, 4, 1, 4), (3, 4, 1, 5), (3, 4, 1, 6), (3, 4, 2, 4), (3, 4, 2, 5), (3, 4, 2, 6), (3, 4, 3, 4), (3, 4, 3, 5), (3, 4, 3, 6), (3, 5, 1, 4), (3, 5, 1, 5), (3, 5, 1, 6), (3, 5, 2, 4), (3, 5, 2, 5), (3, 5, 2, 6), (3, 5, 3, 4), (3, 5, 3, 5), (3, 5, 3, 6), (3, 6, 1, 4), (3, 6, 1, 5), (3, 6, 1, 6), (3, 6, 2, 4), (3, 6, 2, 5), (3, 6, 2, 6), (3, 6, 3, 4), (3, 6, 3, 5), (3, 6, 3, 6)]
[(1, 4, 1, 4), (1, 4, 1, 5), (1, 4, 1, 6), (1, 4, 2, 4), (1, 4, 2, 5), (1, 4, 2, 6), (1, 4, 3, 4), (1, 4, 3, 5), (1, 4, 3, 6), (1, 5, 1, 4), (1, 5, 1, 5), (1, 5, 1, 6), (1, 5, 2, 4), (1, 5, 2, 5), (1, 5, 2, 6), (1, 5, 3, 4), (1, 5, 3, 5), (1, 5, 3, 6), (1, 6, 1, 4), (1, 6, 1, 5), (1, 6, 1, 6), (1, 6, 2, 4), (1, 6, 2, 5), (1, 6, 2, 6), (1, 6, 3, 4), (1, 6, 3, 5), (1, 6, 3, 6), (2, 4, 1, 4), (2, 4, 1, 5), (2, 4, 1, 6), (2, 4, 2, 4), (2, 4, 2, 5), (2, 4, 2, 6), (2, 4, 3, 4), (2, 4, 3, 5), (2, 4, 3, 6), (2, 5, 1, 4), (2, 5, 1, 5), (2, 5, 1, 6), (2, 5, 2, 4), (2, 5, 2, 5), (2, 5, 2, 6), (2, 5, 3, 4), (2, 5, 3, 5), (2, 5, 3, 6), (2, 6, 1, 4), (2, 6, 1, 5), (2, 6, 1, 6), (2, 6, 2, 4), (2, 6, 2, 5), (2, 6, 2, 6), (2, 6, 3, 4), (2, 6, 3, 5), (2, 6, 3, 6), (3, 4, 1, 4), (3, 4, 1, 5), (3, 4, 1, 6), (3, 4, 2, 4), (3, 4, 2, 5), (3, 4, 2, 6), (3, 4, 3, 4), (3, 4, 3, 5), (3, 4, 3, 6), (3, 5, 1, 4), (3, 5, 1, 5), (3, 5, 1, 6), (3, 5, 2, 4), (3, 5, 2, 5), (3, 5, 2, 6), (3, 5, 3, 4), (3, 5, 3, 5), (3, 5, 3, 6), (3, 6, 1, 4), (3, 6, 1, 5), (3, 6, 1, 6), (3, 6, 2, 4), (3, 6, 2, 5), (3, 6, 2, 6), (3, 6, 3, 4), (3, 6, 3, 5), (3, 6, 3, 6)]

repeat(object[, times])

object:任意合法物件

times: 可選,object物件產生的次數, 當不傳入times,則無限循環

傳回一個迭代器,根據times重複產生object物件。

from itertools import repeat


str_data = "assd"
print(repeat(str_data))
print(list(repeat(str_data, 4)))


list_data = [1, 2, 4]
print(repeat(list_data))
print(list(repeat(list_data, 4)))

dict_data = {"a": 1, "b": 2}
print(repeat(dict_data))
print(list(repeat(dict_data, 4)))
#输出
repeat(&#39;assd&#39;)
[&#39;assd&#39;, &#39;assd&#39;, &#39;assd&#39;, &#39;assd&#39;]
repeat([1, 2, 4])
[[1, 2, 4], [1, 2, 4], [1, 2, 4], [1, 2, 4]]
repeat({&#39;a&#39;: 1, &#39;b&#39;: 2})
[{&#39;a&#39;: 1, &#39;b&#39;: 2}, {&#39;a&#39;: 1, &#39;b&#39;: 2}, {&#39;a&#39;: 1, &#39;b&#39;: 2}, {&#39;a&#39;: 1, &#39;b&#39;: 2}]

starmap(function, iterable)

function: 作用域迭代器物件元素的函數

iterable: 可迭代物件

#傳回一個迭代器, 將函數作用與可迭代對象的所有元素(所有元素必須要是可迭代對象,即使只有一個值,也需要使用可迭代對象包裹,例如元組(1, ))中,與map函數類似;當function參數與可迭代物件元素一致時,使用元組代替元素,例如pow(a, b),對應的是[(2,3), (3,3)]。

map與starmap的差別在於,map我們一般會操作一個function只有一個參數的情況,starmap可以操作function多個參數的情況。

from itertools import starmap


list_data = [1, 2, 3, 4, 5]
list_data2 = [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5)]
list_data3 = [(1,), (2,), (3,), (4,), (5,)]

print(list(starmap(lambda x, y: x + y, list_data2)))
print(list(map(lambda x: x * x, list_data)))
print(list(starmap(lambda x: x * x, list_data)))
print(list(starmap(lambda x: x * x, list_data3)))
#输出
[2, 4, 6, 8, 10]
[1, 4, 9, 16, 25]
Traceback (most recent call last):
  File "c:\Users\ts\Desktop\2022.7\2022.7.22\test.py", line 65, in <module>
    print(list(starmap(lambda x: x * x, list_data)))
TypeError: &#39;int&#39; object is not iterable

takewhile(predicate, iterable)

predicate:判断条件,为真就返回

iterable: 可迭代对象

当predicate为真时返回元素,需要注意的是,当第一个元素不为True时,则后面的无论结果如何都不会返回,找的前多少个为True的元素。

from itertools import takewhile
#学习中遇到问题没人解答?小编创建了一个Python学习交流群:725638078

list_data = [1, 5, 4, 6, 2, 3]
print(list(takewhile(lambda x: x > 0, list_data)))
print(list(takewhile(lambda x: x > 1, list_data)))

zip_longest(*iterables, fillvalue=None)

iterables:可迭代对象

fillvalue:当长度超过时,缺省值、默认值, 默认为None

返回迭代器, 可迭代对象元素一一对应生成元组,当两个可迭代对象长度不一致时,会按照最长的有元素输出并使用fillvalue补充,是zip的反向扩展,zip为最小长度输出。

from itertools import zip_longest

list_data = [1, 2, 3]
list_data2 = ["a", "b", "c", "d"]
print(list(zip_longest(list_data, list_data2, fillvalue="-")))
print(list(zip_longest(list_data, list_data2)))
print(list(zip(list_data, list_data2)))

[(1, &#39;a&#39;), (2, &#39;b&#39;), (3, &#39;c&#39;), (&#39;-&#39;, &#39;d&#39;)]
[(1, &#39;a&#39;), (2, &#39;b&#39;), (3, &#39;c&#39;), (None, &#39;d&#39;)]
[(1, &#39;a&#39;), (2, &#39;b&#39;), (3, &#39;c&#39;)]

总结

accumulate(iterable: Iterable, func: None, initial:None):

进行可迭代对象元素的累计运算,可以设置初始值,类似于reduce,相比较reduce,accumulate可以输出中间过程的值,reduce只能输出最后结果,且accumulate性能略好于reduce。

chain(*iterables)

依次输出迭代器中的元素,不会循环输出,有多少输出多少。当输出字典元素时,默认会输出字典的键;而对于列表,则相当于使用extend函数。

combinations(iterable: Iterable, r):

抽取可迭代对象的子序列,其实就是排列组合,不过只返回有序、不重复的子序列,以元组形式呈现。

combinations_with_replacement(iterable: Iterable, r)

类似于combinations,从可迭代对象中提取子序列,但是返回的子序列是无序且不重复的,以元组的形式呈现。

compress(data: Iterable, selectors: Iterable)

根据selectors中的元素是否为True或者False返回可迭代对象的合法元素,selectors为str时,都为True,并且只会决定长度。

count(start, step):

从start开始安装step不断生成元素,是无限循环的,最好控制输出个数或者使用next(),send()等获取、设置结果

cycle(iterable)

循环输出可迭代对象的元素,相当于对chain函数进行无限循环。建议控制输出数据的数量,或使用next()、send()等函数获取或设置返回结果。

dropwhile(predicate, iterable)

根据predicate是否为False来返回可迭代器元素,predicate可以为函数, 返回的是第一个False及之后的所有元素,不管后面的元素是否为True或者False。这个函数适用于舍弃迭代器或可迭代对象的开头部分,比如在写入文件时忽略文档注释

Python中itertools模組如何使用

filterfalse(predicate, iterable)

类似于filter方法,返回所有满足predicate条件的元素,作为一个可迭代对象。

groupby(iterable, key=None)

输出连续符合key要求的键值对,默认为x == x。

islice(iterable, stop)\islice(iterable, start, stop[, step])

对可迭代对象进行切片,和普通切片类似,但是这个不支持负数。这种方法适用于迭代对象的切片,比如你需要获取文件中的某几行内容

pairwise(iterable)

返回连续的重叠对象(两个元素), 少于两个元素返回空,不返回。

permutations(iterable, r=None)

从可迭代对象中抽取子序列,与combinations类似,不过抽取的子序列是无序、可重复。

product(*iterables, repeat=1)

输出可迭代对象的笛卡尔积,类似于排序组合,不可重复,是两个或者多个可迭代对象进行操作,当是一个可迭代对象时,则返回元素,以元组形式返回。

repeat(object[, times])

重复返回object对象,默认时无限循环

starmap(function, iterable)

批量操作可迭代对象中的元素,操作的可迭代对象中的元素必须也要是可迭代对象,与map类似,但是可以对类似于多元素的元组进行操作。

takewhile(predicate, iterable)

返回前多少个predicate为True的元素,如果第一个为False,则直接输出一个空。

Python中itertools模組如何使用

zip_longest(*iterables, fillvalue=None)

将可迭代对象中的元素一一对应,组成元组形式存储,与zip方法类似,不过zip是取最短的,而zip_longest是取最长的,缺少的使用缺省值。

以上是Python中itertools模組如何使用的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除