[這段代碼很Pythonic]相見恨晚的itertools庫

前言

最近事情不是不少,想寫一些技術文章分享給你們,同時也對本身一段時間來碎片化接受的知識進行一下梳理,所謂寫清楚才能說清楚,說清楚才能想清楚,就是這個道理了。python

不少人都致力於把Python代碼寫得更Pythonic,一來更符合規範且容易閱讀,二來通常Pythonic的代碼在執行上也更有效率。今天就先給你們介紹一下Python的系統庫itertools。數據結構

itertools庫

迭代器(生成器)在Python中是一種很經常使用也很好用的數據結構,比起列表(list)來講,迭代器最大的優點就是延遲計算,按需使用,從而提升開發體驗和運行效率,以致於在Python 3中map,filter等操做返回的再也不是列表而是迭代器。函數

話雖這麼說但你們平時用到的迭代器大概只有range了,而經過iter函數把列表對象轉化爲迭代器對象又有點畫蛇添足,這時候咱們今天的主角itertools就該上場了。code

使用itertools

itertools中的函數大可能是返回各類迭代器對象,其中不少函數的做用咱們平時要寫不少代碼才能達到,而在運行效率上反而更低,畢竟人家是系統庫。對象

itertools.accumulate

簡單來講就是累加。ip

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

itertools.chain

鏈接多個列表或者迭代器。開發

>>> 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

求列表或生成器中指定數目的元素不重複的全部組合it

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

itertools.combinations_with_replacement

容許重複元素的組合io

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

itertools.compress

按照真值表篩選元素class

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

itertools.count

就是一個計數器,能夠指定起始位置和步長

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

itertools.cycle

循環指定的列表和迭代器

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

itertools.dropwhile

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

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

itertools.filterfalse

保留對應真值爲False的元素

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

itertools.groupby

按照分組函數的值對元素進行分組

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

itertools.islice

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

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

itertools.permutations

產生指定數目的元素的全部排列(順序有關)

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

itertools.product

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

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

itertools.repeat

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

>>> x = itertools.repeat(0, 5)
>>> print(list(x))
[0, 0, 0, 0, 0]

itertools.starmap

相似map

>>> x = itertools.starmap(str.islower, 'aBCDefGhI')
>>> print(list(x))
[True, False, False, False, True, True, False, True, False]

itertools.takewhile

與dropwhile相反,保留元素直至真值函數值爲假。

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

itertools.tee

這個函數我也不是很懂,彷佛是生成指定數目的迭代器

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

itertools.zip_longest

相似於zip,不過已較長的列表和迭代器的長度爲準

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

結語

大概就總結到這裏,不過老實說Python的各類語言特性和庫仍是要多用才能熟練,最終達到隨手拈來的程度,裝逼的說就是由術入道。

相關文章
相關標籤/搜索