激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

腳本之家,腳本語言編程技術(shù)及教程分享平臺!
分類導(dǎo)航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務(wù)器之家 - 腳本之家 - Python - 關(guān)于Python 內(nèi)置庫 itertools

關(guān)于Python 內(nèi)置庫 itertools

2022-01-13 00:31tuicool Python

今天得這篇文章就來給大家介紹一下Python的系統(tǒng)庫itertools的 相關(guān)資料,需要的小伙伴可以參考下面文章的具體內(nèi)容

前言:

最近事情不是很多,想寫一些技術(shù)文章分享給大家,同時也對自己一段時間來碎片化接受的知識進行一下梳理,所謂寫清楚才能說清楚,說清楚才能想清楚,就是這個道理了。

很多人都致力于把Python代碼寫得更Pythonic,一來更符合規(guī)范且容易閱讀,二來一般Pythonic的代碼在執(zhí)行上也更有效率。今天就先給大家介紹一下Python的系統(tǒng)庫itertools

1、itertools庫

迭代器(生成器)在Python中是一種很常用也很好用的數(shù)據(jù)結(jié)構(gòu),比起列表(list)來說,迭代器最大的優(yōu)勢就是延遲計算,按需使用,從而提高開發(fā)體驗和運行效率,以至于在Python 3中map,filter等操作返回的不再是列表而是迭代器。

話雖這么說但大家平時用到的迭代器大概只有range了,而通過iter函數(shù)把列表對象轉(zhuǎn)化為迭代器對象又有點多此一舉,這時候我們今天的主角itertools就該上場了。

2、使用itertools

itertools中的函數(shù)大多是返回各種迭代器對象,其中很多函數(shù)的作用我們平時要寫很多代碼才能達到,而在運行效率上反而更低,畢竟人家是系統(tǒng)庫。

3、itertools.accumulate

簡單來說就是累加。

?
1
2
3
4
>>> import itertools 
>>> x = itertools.accumulate(range(10)) 
>>> print(list(x)) 
[0, 1, 3, 6, 10, 15, 21, 28, 36, 45]

4、itertools.chain

連接多個列表或者迭代器。

?
1
2
3
4
>>> x = itertools.chain(range(3), range(4), [3,2,1]) 
>>> print(list(x)) 
[0, 1, 2, 0, 1, 2, 3, 3, 2, 1]
itertools.combinations

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

?
1
2
3
>>> x = itertools.combinations(range(4), 3
>>> print(list(x)) 
[(0, 1, 2), (0, 1, 3), (0, 2, 3), (1, 2, 3)]

5、itertools.combinations_with_replacement

允許重復(fù)元素的組合

?
1
2
3
>>> x = itertools.combinations_with_replacement('ABC', 2
>>> print(list(x)) 
[('A', 'A'), ('A', 'B'), ('A', 'C'), ('B', 'B'), ('B', 'C'), ('C', 'C')] 

6、itertools.compress

按照真值表篩選元素

?
1
2
3
>>> x = itertools.compress(range(5), (True, False, True, True, False)) 
>>> print(list(x)) 
[0, 2, 3]

7、itertools.count

就是一個計數(shù)器,可以指定起始位置和步長

?
1
2
3
>>> x = itertools.count(start=20, step=-1
>>> print(list(itertools.islice(x, 0, 10, 1))) 
[20, 19, 18, 17, 16, 15, 14, 13, 12, 11]

8、itertools.cycle

循環(huán)指定的列表和迭代器

?
1
2
3
>>> x = itertools.cycle('ABC'
>>> print(list(itertools.islice(x, 0, 10, 1))) 
['A', 'B', 'C', 'A', 'B', 'C', 'A', 'B', 'C', 'A']

9、itertools.dropwhile

按照真值函數(shù)丟棄掉列表和迭代器前面的元素

?
1
2
3
>>> x = itertools.dropwhile(lambda e: e < 5, range(10)) 
>>> print(list(x)) 
[5, 6, 7, 8, 9]

10、itertools.filterfalse

保留對應(yīng)真值為False的元素

?
1
2
3
>>> x = itertools.filterfalse(lambda e: e < 5, (1, 5, 3, 6, 9, 4)) 
>>> print(list(x)) 
[5, 6, 9]

11、itertools.groupby

按照分組函數(shù)的值對元素進行分組

?
1
2
3
4
5
6
>>> x = itertools.groupby(range(10), lambda x: x < 5 or x > 8)                                                                                              
>>> for condition, numbers in x:                                                
...     print(condition, list(numbers))                                                                                                        
True [0, 1, 2, 3, 4]                                                              
False [5, 6, 7, 8]                                                               
True [9]

12、itertools.islice

上文使用過的函數(shù),對迭代器進行切片

?
1
2
3
>>> x = itertools.islice(range(10), 0, 9, 2
>>> print(list(x)) 
[0, 2, 4, 6, 8]

13、itertools.permutations

產(chǎn)生指定數(shù)目的元素的所有排列(順序有關(guān))

?
1
2
3
>>> x = itertools.permutations(range(4), 3
>>> print(list(x)) 
[(0, 1, 2), (0, 1, 3), (0, 2, 1), (0, 2, 3), (0, 3, 1), (0, 3, 2), (1, 0, 2), (1, 0, 3), (1, 2, 0), (1, 2, 3), (1, 3, 0), (1, 3, 2), (2, 0, 1), (2, 0,3), (2, 1, 0), (2, 1, 3), (2, 3, 0), (2, 3, 1), (3, 0, 1), (3, 0, 2), (3, 1, 0), (3, 1, 2), (3, 2, 0), (3, 2, 1)]

14、itertools.product

產(chǎn)生多個列表和迭代器的(積)

?
1
2
3
4
>>> x = itertools.product('ABC', range(3)) 
>>> 
>>> print(list(x)) 
[('A', 0), ('A', 1), ('A', 2), ('B', 0), ('B', 1), ('B', 2), ('C', 0), ('C', 1), ('C', 2)]

15、itertools.repeat

簡單的生成一個擁有指定數(shù)目元素的迭代器

?
1
2
3
>>> x = itertools.repeat(0, 5
>>> print(list(x)) 
[0, 0, 0, 0, 0]

16、itertools.starmap

類似map

?
1
2
3
>>> x = itertools.starmap(str.islower, 'aBCDefGhI'
>>> print(list(x)) 
[True, False, False, False, True, True, False, True, False

17、itertools.takewhile

dropwhile相反,保留元素直至真值函數(shù)值為假。

?
1
2
3
>>> x = itertools.takewhile(lambda e: e < 5, range(10)) 
>>> print(list(x)) 
[0, 1, 2, 3, 4]

18、itertools.tee

這個函數(shù)我也不是很懂,似乎是生成指定數(shù)目的迭代器

?
1
2
3
4
5
6
>>> x = itertools.tee(range(10), 2
>>> for letters in x: 
...     print(list(letters)) 
... 
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

19、itertools.zip_longest

類似于zip,不過已較長的列表和迭代器的長度為準(zhǔn)

?
1
2
3
4
5
6
>>> x = itertools.zip_longest(range(3), range(5)) 
>>> y = zip(range(3), range(5)) 
>>> print(list(x)) 
[(0, 0), (1, 1), (2, 2), (None, 3), (None, 4)] 
>>> print(list(y)) 
[(0, 0), (1, 1), (2, 2)]

結(jié)語:

到此這篇關(guān)于關(guān)于Python 內(nèi)置庫 itertools的文章就介紹到這了,更多相關(guān)Python內(nèi)置庫itertools內(nèi)容請搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!

原文鏈接:https://www.tuicool.com/articles/yQziY3m

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: av之家在线观看 | 加勒比婷婷色综合久久 | 黄色网址免费在线 | 久久综合婷婷香五月 | 精品国产一区二区三区在线观看 | 久久国产夫妻视频 | 91福利在线观看 | 99精品视频网站 | 99久久精品日本一区二区免费 | 中文字幕在线观看视频一区 | 久久九九热re6这里有精品 | 日韩在线播放一区二区 | 国产精品岛国久久久久久久 | 97中文字幕在线观看 | 久久国产精品久久久久久久久久 | www.99tv | 亚洲精品久久久久久久久久久 | 黄色影院av | 成人免费观看在线视频 | 免费黄色小网站 | 日本欧美一区二区三区在线播 | 欧美成人二区 | 国产精品视频在线观看免费 | 亚洲免费永久 | 国产视频在线观看免费 | xfplay噜噜av | av中文字幕免费在线观看 | 久久99精品久久久久久秒播放器 | 一区二区国产在线 | 亚州综合图片 | 玖草 | 午夜a狂野欧美一区二区 | 中文在线国产 | 久久国产精品二国产精品中国洋人 | 亚洲精品aaaaa | 欧产日产国产精品乱噜噜 | 欧美日韩亚洲一区二区三区 | 欧美精品18| 九九热九九热 | 91网站在线播放 | 黄色网址入口 |