網站首頁 編程語言 正文
前言:
Python的內建模塊itertools
提供了非常有用的用于操作迭代對象的函數,itertools提供的幾個“無限”迭代器
1、count()創建自然數序列
import itertools ? n = 0 natuals = itertools.count(1) for m in natuals: ? ? print(m)
? ?因為count(n)
會創建一個無限的迭代器,起始數字為n,迭代至無窮大。所以上述代碼會打印出自然數序列,根本停不下來,只能按Ctrl+C退出。
運行結果:
0 1 2 3 4 5 6 7 ...
2、cycle()創建序列循環
? ? ? ? cycle()
會把傳入的一個序列無限重復下去。
示例1、循環列表中的內容:
natuals = itertools.cycle([1, 2, 3]) for m in natuals: ? ? print(m)
運行結果:
1 2 3 1 2 3 1 2 3 ...
示例2、字符串中字符循環:
import itertools cs = itertools.cycle('ABC') # 注意字符串也是序列的一種 for c in cs: ? ? print(c)
運行結果:
A B C A B C A B C ...
3、repeat()把一個元素有限次重復
? ? ? ?repeat()
負責把一個元素無限重復下去(默認行為),不過如果提供第二個參數就可以限定重復次數。
ns = itertools.repeat('A', 3) for n in ns: ? ? print(n)
運行結果:
A A A ...
4、takewhile()根據條件判斷來截取出一個有限的序列
? ? ? ? 無限序列只有在for迭代時才會無限地迭代下去,如果只是創建了一個迭代對象,它不會事先把無限個元素生成出來,事實上也不可能在內存中創建無限多個元素。無限序列雖然可以無限迭代下去,但是通常我們會通過takewhile()等函數根據條件判斷來截取出一個有限的序列。
import itertools ? natuals = itertools.count(1) ns = itertools.takewhile(lambda x: x <= 10, natuals) print(list(ns))
運行結果:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
迭代器測試:
? ? ? ? 對于有限數據的迭代器,每調用一次,迭代器內數據彈出。
import itertools ? natuals = itertools.count(1) ns = itertools.takewhile(lambda x: x <= 3, natuals) for n in ns: ? ? print(n) ? print(list(ns))
運行結果:
1 2 3 []
5、chain()迭代對象串聯
? ? ? ? chain()可以把一組迭代對象串聯起來,形成一個更大的迭代器。
for c in itertools.chain('ABC', 'XYZ'): ? ? print(c)
運行結果:
'A' 'B' 'C' 'X' 'Y' 'Z'
6、groupby()迭代器元素分組
? ? ? ?groupby()
把迭代器中相鄰的重復元素挑出來放在一起,更多的用于字符串。
for key, group in itertools.groupby('AAABBBCCAAA'): ? ? print(key, list(group))
運行結果:
A ['A', 'A', 'A']
B ['B', 'B', 'B']
C ['C', 'C']
A ['A', 'A', 'A']
? ? ? ? 實際上挑選規則是通過函數完成的,只要作用于函數的兩個元素返回的值相等,這兩個元素就被認為是在一組的,而函數返回值作為組的key。如果我們要忽略大小寫分組,就可以讓元素'A'和'a'都返回相同的key。
for key, group in itertools.groupby('AaaBBbcCAAa', lambda c: c.upper()): ? ? print(key, list(group))
運行結果:
A ['A', 'a', 'a']
B ['B', 'B', 'b']
C ['c', 'C']
A ['A', 'A', 'a']
原文鏈接:https://blog.csdn.net/weixin_34910922/article/details/122831611
相關推薦
- 2022-06-14 ASP.NET?Core?MVC中的布局(Layout)_基礎應用
- 2023-09-17 Android 獲取Wifi列表詳解(包含動態權限申請)
- 2022-06-09 ASP.NET?Core中的Configuration配置一_基礎應用
- 2022-03-24 Sublime?Text3安裝Go語言相關插件gosublime時搜不到gosublime的解決方法
- 2023-11-19 樹莓派/arm設備上安裝火狐Firefox瀏覽器
- 2022-04-08 Python裝飾器中@property使用詳解_python
- 2022-07-26 Python使用psutil獲取系統信息_python
- 2022-10-09 C#使用Enum.TryParse()實現枚舉安全轉換_C#教程
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支