日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

itertools库

發布時間:2025/4/16 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 itertools库 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前言

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

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

itertools庫

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

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

使用itertools

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

itertools.accumulate

簡單來說就是累加。

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

?

itertools.chain

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

Python
1 2 3 >>> 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

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

Python
1 2 3 >>> 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

允許重復元素的組合

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

?

itertools.compress

按照真值表篩選元素

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

?

itertools.count

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

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

?

itertools.cycle

循環指定的列表和迭代器

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

?

itertools.dropwhile

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

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

?

itertools.filterfalse

保留對應真值為False的元素

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

?

itertools.groupby

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

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

?

itertools.islice

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

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

?

itertools.permutations

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

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,

轉載于:https://www.cnblogs.com/Ironboy/p/8732461.html

總結

以上是生活随笔為你收集整理的itertools库的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。