一文詳解Python中itertools模塊的使用方法
itertools — 為高效循環(huán)而創(chuàng)建迭代器的函數(shù)
accumulate(iterable: Iterable, func: None, initial:None)
iterable:需要操作的可迭代對(duì)象
func:對(duì)可迭代對(duì)象需要操作的函數(shù),必須包含兩個(gè)參數(shù)
initial: 累加的開始值
對(duì)可迭代對(duì)象進(jìn)行累計(jì)或者通過func實(shí)現(xiàn)雙目運(yùn)算,當(dāng)指定func的時(shí)候需要兩個(gè)參數(shù)。返回的是迭代器,與這個(gè)方法類似的就是functools下的reduce,reduce和accumulate都是累計(jì)進(jìn)行操作,不同的是reduce只會(huì)返回最后的元素,而accumulate會(huì)顯示所有的元素,包含中間的元素,對(duì)比如下:
| 區(qū)別 | reduce | accumulate |
|---|---|---|
| 返回值 | 返回的是一個(gè)元素 | 返回的是一個(gè)迭代器(包含中間處理的元素) |
| 所屬模塊 | functools | itertools |
| 性能 | 略差 | 比reduce好一些 |
| 初始值 | 可以設(shè)置初始值 | 可以設(shè)置初始值 |
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
由上述結(jié)果可知,accumulate比reduce性能稍好一些,而且還能輸出中間的處理過程。
chain(*iterables)
iterables:接收多個(gè)可迭代對(duì)象
依次返回多個(gè)迭代對(duì)象的元素,返回的是一個(gè)迭代器,對(duì)于字典輸出元素時(shí),默認(rèn)會(huì)輸出字典的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:需要操作的可迭代對(duì)象
r: 抽取的子序列元素的個(gè)數(shù)
操作可迭代對(duì)象,根據(jù)所需抽取的子序列個(gè)數(shù)返回子序列,子序列中的元素也是有序、不可重復(fù)并且是以元組的形式呈現(xiàn)的。
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)類似,不過區(qū)別在于,combinations_with_replacement的子序列的元素可以重復(fù),也是有序的,具體如下:
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:需要操作的可迭代對(duì)象
selectors:判斷真值的可迭代對(duì)象,不能時(shí)str,最好是列表、元組、之類的
根據(jù)selectors中的元素是否為true來輸出data中對(duì)應(yīng)索引的元素,以最短的為準(zhǔn),返回一個(gè)迭代器。
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: 自開始元素增長(zhǎng)的步長(zhǎng)
返回一個(gè)迭代器,從start按照步長(zhǎng)遞增,不會(huì)一次性生成,最好使用next()進(jìn)行元素的遞歸的獲取。
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: 需要循環(huán)輸出的可迭代對(duì)象
返回一個(gè)迭代器,循環(huán)輸出可迭代對(duì)象的元素。于count一樣,最好不要將結(jié)果轉(zhuǎn)換為可迭代對(duì)象,因?yàn)槭茄h(huán),所以建議使用next()或者for循環(huán)獲取元素。
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:是否舍棄元素的標(biāo)準(zhǔn)
iterable: 可迭代對(duì)象
返回一個(gè)迭代器,根據(jù)predicate是否為True來舍棄元素。當(dāng)predicate為False時(shí),后面的元素不管是否為True都會(huì)輸出。
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:是否舍棄元素的標(biāo)準(zhǔn)
iterable: 可迭代對(duì)象
返回一個(gè)迭代器, 根據(jù)predicate是否為False判斷輸出,對(duì)所有元素進(jìn)行操作。類似于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
由上述結(jié)果看出,filterfalse與filter性能相差不大
groupby(iterable, key=None)
iterable: 可迭代對(duì)象
key: 可選,需要對(duì)元素進(jìn)行判斷的條件, 默認(rèn)為x == x。
返回一個(gè)迭代器,根據(jù)key返回連續(xù)的鍵和組(連續(xù)符合key條件的元素)。
注意使用groupby進(jìn)行分組前需要對(duì)其進(jìn)行排序。
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 ['b']
a ['a']
b ['b']
a ['a']
d ['d']
a ['a']
a ['a', 'a', 'a']
b ['b', 'b', 'b']
c ['c']
d ['d']
1
2
a
True ['1', '2']
3
False ['a']
4
d
True ['3', '4']
5
False ['d']
True ['5']
islice(iterable, stop)\islice(iterable, start, stop[, step])
iterable: 需要操作的可迭代對(duì)象
start: 開始操作的索引位置
stop: 結(jié)束操作的索引位置
step: 步長(zhǎng)
返回一個(gè)迭代器。類似于切片,但是其索引不支持負(fù)數(shù)。
from itertools import islice
import time
list_data = [1, 5, 4, 2, 7]
#學(xué)習(xí)中遇到問題沒人解答?小編創(chuàng)建了一個(gè)Python學(xué)習(xí)交流群: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]
由上述結(jié)果可以看出,切片性能比islice性能稍好一些。
pairwise(iterable)
需要操作的可迭代對(duì)象
返回一個(gè)迭代器, 返回可迭代對(duì)象中的連續(xù)重疊對(duì),少于兩個(gè)返回空。
from itertools import pairwise
str_data = "asdfweffva"
list_data = [1, 2, 5, 76, 8]
print(list(pairwise(str_data)))
print(list(pairwise(list_data)))
#輸出
[('a', 's'), ('s', 'd'), ('d', 'f'), ('f', 'w'), ('w', 'e'), ('e', 'f'), ('f', 'f'), ('f', 'v'), ('v', 'a')]
[(1, 2), (2, 5), (5, 76), (76, 8)]
permutations(iterable, r=None)
iterable: 需要操作的可迭代對(duì)象
r: 抽取的子序列
與combinations類似,都是抽取可迭代對(duì)象的子序列,不過,permutations是不可重復(fù),無序的, 與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))
(('a', 's'), ('a', 'd'), ('a', 'f'), ('a', 'g'), ('a', 'h'), ('s', 'a'), ('s', 'd'), ('s', 'f'), ('s', 'g'), ('s', 'h'), ('d', 'a'), ('d', 's'), ('d', 'f'), ('d', 'g'), ('d', 'h'), ('f', 'a'), ('f', 's'), ('f', 'd'), ('f', 'g'), ('f', 'h'), ('g', 'a'), ('g', 's'), ('g', 'd'), ('g', 'f'), ('g', 'h'), ('h', 'a'), ('h', 's'), ('h', 'd'), ('h', 'f'), ('h', 'g'))
product(*iterables, repeat=1)
iterables: 可迭代對(duì)象,可以為多個(gè)
repeat: 可迭代對(duì)象的重復(fù)次數(shù),也就是復(fù)制的次數(shù)
返回迭代器。生成可迭代對(duì)象的笛卡爾積, 類似于兩個(gè)或多個(gè)可迭代對(duì)象的排列組合。與zip函數(shù)很像,但是zip是元素的一對(duì)一對(duì)應(yīng)關(guān)系,而product則是一對(duì)多的關(guān)系。
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))) # 如下兩個(gè)含義是一樣的,都是將可迭代對(duì)象復(fù)制一份, 很方便的進(jìn)行同列表的操作 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:任意合法對(duì)象
times: 可選,object對(duì)象生成的次數(shù), 當(dāng)不傳入times,則無限循環(huán)
返回一個(gè)迭代器,根據(jù)times重復(fù)生成object對(duì)象。
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('assd')
['assd', 'assd', 'assd', 'assd']
repeat([1, 2, 4])
[[1, 2, 4], [1, 2, 4], [1, 2, 4], [1, 2, 4]]
repeat({'a': 1, 'b': 2})
[{'a': 1, 'b': 2}, {'a': 1, 'b': 2}, {'a': 1, 'b': 2}, {'a': 1, 'b': 2}]
starmap(function, iterable)
function: 作用域迭代器對(duì)象元素的函數(shù)
iterable: 可迭代對(duì)象
返回一個(gè)迭代器, 將函數(shù)作用與可迭代對(duì)象的所有元素(所有元素必須要是可迭代對(duì)象,即使只有一個(gè)值,也需要使用可迭代對(duì)象包裹,例如元組(1, ))中,與map函數(shù)類似;當(dāng)function參數(shù)與可迭代對(duì)象元素一致時(shí),使用元組代替元素,例如pow(a, b),對(duì)應(yīng)的是[(2,3), (3,3)]。
map與starmap的區(qū)別在于,map我們一般會(huì)操作一個(gè)function只有一個(gè)參數(shù)的情況,starmap可以操作function多個(gè)參數(shù)的情況。
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: 'int' object is not iterable
takewhile(predicate, iterable)
predicate:判斷條件,為真就返回
iterable: 可迭代對(duì)象
當(dāng)predicate為真時(shí)返回元素,需要注意的是,當(dāng)?shù)谝粋€(gè)元素不為True時(shí),則后面的無論結(jié)果如何都不會(huì)返回,找的前多少個(gè)為True的元素。
from itertools import takewhile #學(xué)習(xí)中遇到問題沒人解答?小編創(chuàng)建了一個(gè)Python學(xué)習(xí)交流群: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:可迭代對(duì)象
fillvalue:當(dāng)長(zhǎng)度超過時(shí),缺省值、默認(rèn)值, 默認(rèn)為None
返回迭代器, 可迭代對(duì)象元素一一對(duì)應(yīng)生成元組,當(dāng)兩個(gè)可迭代對(duì)象長(zhǎng)度不一致時(shí),會(huì)按照最長(zhǎng)的有元素輸出并使用fillvalue補(bǔ)充,是zip的反向擴(kuò)展,zip為最小長(zhǎng)度輸出。
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, 'a'), (2, 'b'), (3, 'c'), ('-', 'd')]
[(1, 'a'), (2, 'b'), (3, 'c'), (None, 'd')]
[(1, 'a'), (2, 'b'), (3, 'c')]
總結(jié)
accumulate(iterable: Iterable, func: None, initial:None):
進(jìn)行可迭代對(duì)象元素的累計(jì)運(yùn)算,可以設(shè)置初始值,類似于reduce,相比較reduce,accumulate可以輸出中間過程的值,reduce只能輸出最后結(jié)果,且accumulate性能略好于reduce。
chain(*iterables)
依次輸出迭代器中的元素,不會(huì)循環(huán)輸出,有多少輸出多少。對(duì)于字典輸出元素時(shí),默認(rèn)會(huì)輸出字典的key;對(duì)于列表來說相當(dāng)于是extend。
combinations(iterable: Iterable, r):
抽取可迭代對(duì)象的子序列,其實(shí)就是排列組合,不過只返回有序、不重復(fù)的子序列,以元組形式呈現(xiàn)。
combinations_with_replacement(iterable: Iterable, r)
抽取可迭代對(duì)象的子序列,與combinations類似,不過返回?zé)o序、不重復(fù)的子序列,以元組形式呈現(xiàn)。
compress(data: Iterable, selectors: Iterable)
根據(jù)selectors中的元素是否為True或者False返回可迭代對(duì)象的合法元素,selectors為str時(shí),都為True,并且只會(huì)決定長(zhǎng)度。
count(start, step):
從start開始安裝step不斷生成元素,是無限循環(huán)的,最好控制輸出個(gè)數(shù)或者使用next(),send()等獲取、設(shè)置結(jié)果
cycle(iterable)
依次輸出可迭代對(duì)象的元素,是無限循環(huán)的,相當(dāng)于是chain的循環(huán)。最好控制輸出個(gè)數(shù)或者使用next(),send()等獲取、設(shè)置結(jié)果。
dropwhile(predicate, iterable)
根據(jù)predicate是否為False來返回可迭代器元素,predicate可以為函數(shù), 返回的是第一個(gè)False及之后的所有元素,不管后面的元素是否為True或者False。適用于需要舍棄迭代器或者可迭代對(duì)象的前面一部分內(nèi)容,例如在寫入文件時(shí)忽略文檔注釋

filterfalse(predicate, iterable)
依據(jù)predicate返回可迭代對(duì)象的所有predicate為True的元素,類似于filter方法。
groupby(iterable, key=None)
輸出連續(xù)符合key要求的鍵值對(duì),默認(rèn)為x == x。
islice(iterable, stop)\islice(iterable, start, stop[, step])
對(duì)可迭代對(duì)象進(jìn)行切片,和普通切片類似,但是這個(gè)不支持負(fù)數(shù)。適用于可迭代對(duì)象內(nèi)容的切割,例如你需要獲取一個(gè)文件中的某幾行的內(nèi)容
pairwise(iterable)
返回連續(xù)的重疊對(duì)象(兩個(gè)元素), 少于兩個(gè)元素返回空,不返回。
permutations(iterable, r=None)
從可迭代對(duì)象中抽取子序列,與combinations類似,不過抽取的子序列是無序、可重復(fù)。
product(*iterables, repeat=1)
輸出可迭代對(duì)象的笛卡爾積,類似于排序組合,不可重復(fù),是兩個(gè)或者多個(gè)可迭代對(duì)象進(jìn)行操作,當(dāng)是一個(gè)可迭代對(duì)象時(shí),則返回元素,以元組形式返回。
repeat(object[, times])
重復(fù)返回object對(duì)象,默認(rèn)時(shí)無限循環(huán)
starmap(function, iterable)
批量操作可迭代對(duì)象中的元素,操作的可迭代對(duì)象中的元素必須也要是可迭代對(duì)象,與map類似,但是可以對(duì)類似于多元素的元組進(jìn)行操作。
takewhile(predicate, iterable)
返回前多少個(gè)predicate為True的元素,如果第一個(gè)為False,則直接輸出一個(gè)空。

zip_longest(*iterables, fillvalue=None)
將可迭代對(duì)象中的元素一一對(duì)應(yīng),組成元組形式存儲(chǔ),與zip方法類似,不過zip是取最短的,而zip_longest是取最長(zhǎng)的,缺少的使用缺省值。
到此這篇關(guān)于一文詳解Python中itertools模塊的使用方法的文章就介紹到這了,更多相關(guān)Python itertools模塊內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python創(chuàng)建學(xué)生成績(jī)管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了python創(chuàng)建學(xué)生成績(jī)管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11
Python3使用tracemalloc實(shí)現(xiàn)追蹤mmap內(nèi)存變化
這篇文章主要為大家詳細(xì)介紹了在Python3中如何使用tracemalloc實(shí)現(xiàn)追蹤mmap內(nèi)存變化,文中的示例代碼講解詳細(xì),感興趣的可以了解一下2023-03-03
Python 模擬生成動(dòng)態(tài)產(chǎn)生驗(yàn)證碼圖片的方法
這篇文章主要介紹了Python 模擬生成動(dòng)態(tài)產(chǎn)生驗(yàn)證碼圖片的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
Pandas.DataFrame重置Series的索引index(reset_index)
本文主要介紹了Pandas.DataFrame重置Series的索引index(reset_index),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02
Python實(shí)現(xiàn)一個(gè)簡(jiǎn)單的QQ截圖
大家好,本篇文章主要講的是Python實(shí)現(xiàn)一個(gè)簡(jiǎn)單的QQ截圖,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下的相關(guān)資料2022-02-02

