每周一个 Python 模块 | itertools

简介: Python 标准库模块 itertools 提供了很多方便灵活的迭代器工具,熟练的运用可以极大的提高工作效率。

无限迭代器


itertools.count


count(start=0, step=1)
复制代码


创建一个迭代器,生成从 n 开始的连续整数,如果忽略 n,则从 0 开始计算。示例:


In [2]: for n in itertools.count():
   ...:     if 100000 < n < 100010:
   ...:         print n
   ...:     if n > 1000000:
   ...:         break
   ...:     
100001
100002
100003
100004
100005
100006
100007
100008
100009
复制代码


itertools.cycle


cycle(iterable)
复制代码


把传入的一个序列无限重复下去。示例:


In [6]: count = 0
In [7]: for c in itertools.cycle("AB"):
   ...:     if count > 4:
   ...:         break
   ...:     print c
   ...:     count += 1
   ...:     
A
B
A
B
A
复制代码


itertools.repeat



repeat(object [,times])
复制代码


创建一个迭代器,重复生成 object,times(如果已提供)指定重复计数,如果未提供 times,将无止尽返回该对象。示例:


In [8]: for x in itertools.repeat("hello world", 5):
   ...:     print x
   ...:     
hello world
hello world
hello world
hello world
hello world
复制代码


函数式工具


itertools.ifilteritertools.reduceitertools.imapitertools.izip

与内建函数 filter()reduce()map()zip() 有同样的功能,只是返回一个迭代器而不是一个序列。在 Python3 中被去掉,因为默认的内建函数就是返回一个迭代器。


itertools.ifilterfalse


ifilterfalse(function or None, sequence)
复制代码

python3 为:

filterfalse(function or None, sequence)
复制代码


与 filter 类似,但仅生成 sequence 中 function(item) 为 False 的项。示例:


In [25]: for elem in itertools.ifilterfalse(lambda x: x > 5, [2, 3, 5, 6, 7]):
   ....:     print elem
   ....:     
2
3
5
复制代码


itertools.izip_longest


izip_longest(iter1 [,iter2 [...]], [fillvalue=None])
复制代码


Python3 为:


zip_longest(iter1 [,iter2 [...]], [fillvalue=None])
复制代码


与 zip 类似,但不同的是它会把最长的 iter 迭代完才结束,其他 iter 如果有缺失值则用 fillvalue 填充。示例:


In [33]: for item in itertools.izip_longest('abcd', '12', fillvalue='-'):
   ....:     print item
   ....:     
('a', '1')
('b', '2')
('c', '-')
('d', '-')
复制代码


itertools.starmap


starmap(function, sequence)
复制代码


对序列 sequence 的每个元素作为 function 的参数列表执行,即 function(*item), 返回执行结果的迭代器。只有当 iterable 生成的项适用于这种调用函数的方式时,此函数才有效。示例:


In [35]: seq = [(0, 5), (1, 6), (2, 7), (3, 3), (3, 8), (4, 9)]
In [36]: for item in itertools.starmap(lambda x,y:(x, y, x*y), seq):
    ...:     print "%d * %d = %d" % item
    ...:     
0 * 5 = 0
1 * 6 = 6
2 * 7 = 14
3 * 3 = 9
3 * 8 = 24
4 * 9 = 36
复制代码

itertools.dropwhile

dropwhile(predicate, iterable)
复制代码


创建一个迭代器,只要函数 predicate(item) 为 True,就丢弃 iterable 中的项,如果 predicate 返回 False,就会生成 iterable 中的项和所有后续项。即在条件为false之后的第一次, 返回迭代器中剩下来的项。示例:


In [41]: for item in itertools.dropwhile(lambda x: x<1, [ -1, 0, 1, 2, 3, 4, 1, -2 ]):
    ...:     print item
    ...:     
1
2
3
4
1
-2
复制代码


itertools.takewhile


takewhile(predicate, iterable)
复制代码


与 dropwhile 相反。创建一个迭代器,生成 iterable 中 predicate(item) 为 True 的项,只要 predicate 计算为 False,迭代就会立即停止。示例:

In [28]: for item in itertools.takewhile(lambda x: x < 2, [ -1, 0, 1, 2, 3, 4, 1, -2 ]):
   ....:     print item
   ....:     
-1
0
1
复制代码


组合工具


itertools.chain


chain(*iterables)
复制代码


把一组迭代对象串联起来,形成一个更大的迭代器。示例:

In [9]: for c in itertools.chain('ABC', 'XYZ'):
   ...:     print c
   ...:     
A
B
C
X
Y
Z
复制代码


itertools.product


product(*iterables, repeat=1)
复制代码

创建一个迭代器,生成多个迭代器集合的笛卡尔积,repeat 参数用于指定重复生成序列的次数。示例:


In [6]: for elem in itertools.product((1, 2), ('a', 'b')):
   ...:     print elem
   ...:     
(1, 'a')
(1, 'b')
(2, 'a')
(2, 'b')
In [7]: for elem in itertools.product((1, 2), ('a', 'b'), repeat=2):
   ...:     print elem
   ...:     
(1, 'a', 1, 'a')
(1, 'a', 1, 'b')
(1, 'a', 2, 'a')
(1, 'a', 2, 'b')
(1, 'b', 1, 'a')
(1, 'b', 1, 'b')
(1, 'b', 2, 'a')
(1, 'b', 2, 'b')
(2, 'a', 1, 'a')
(2, 'a', 1, 'b')
(2, 'a', 2, 'a')
(2, 'a', 2, 'b')
(2, 'b', 1, 'a')
(2, 'b', 1, 'b')
(2, 'b', 2, 'a')
(2, 'b', 2, 'b')
复制代码


itertools.permutations


permutations(iterable[, r])
复制代码


返回 iterable 中任意取 r 个元素做排列的元组的迭代器,如果不指定 r,那么序列的长度与 iterable 中的项目数量相同。示例:


In [7]: for elem in itertools.permutations('abc', 2):
   ...:     print elem
   ...:     
('a', 'b')
('a', 'c')
('b', 'a')
('b', 'c')
('c', 'a')
('c', 'b')
In [8]: for elem in itertools.permutations('abc'):
   ...:     print elem
   ...:     
('a', 'b', 'c')
('a', 'c', 'b')
('b', 'a', 'c')
('b', 'c', 'a')
('c', 'a', 'b')
('c', 'b', 'a')
复制代码


itertools.combinations


combinations(iterable, r)
复制代码

与 permutations 类似,但组合不分顺序,即如果 iterable 为 "abc",r 为 2 时,ab 和 ba 则视为重复,此时只放回 ab. 示例:

In [10]: for elem in itertools.combinations('abc', 2):
   ....:     print elem
   ....:     
('a', 'b')
('a', 'c')
('b', 'c')
复制代码


itertools.combinations_with_replacement


combinations_with_replacement(iterable, r)
复制代码


与 combinations 类似,但允许重复值,即如果 iterable 为 "abc",r 为 2 时,会多出

aa, bb, cc. 示例:



In [14]: for elem in itertools.combinations_with_replacement('abc', 2):
   ....:     print elem
   ....:     
('a', 'a')
('a', 'b')
('a', 'c')
('b', 'b')
('b', 'c')
('c', 'c')
复制代码


其他工具


itertools.compress


compress(data, selectors)
复制代码


相当于 bool 选取,只有当 selectors 对应位置的元素为 true 时,才保留 data 中相应位置的元素,否则去除。示例:


In [39]: list(itertools.compress('abcdef', [1, 1, 0, 1, 0, 1]))
Out[39]: ['a', 'b', 'd', 'f']
In [40]: list(itertools.compress('abcdef', [True, False, True]))
Out[40]: ['a', 'c']
复制代码


itertools.groupby


groupby(iterable[, keyfunc])
复制代码


对 iterable 中的元素进行分组。keyfunc 是分组函数,用于对 iterable 的连续项进行分组,如果不指定,则默认对 iterable 中的连续相同项进行分组,返回一个 (key, sub-iterator) 的迭代器。示例:


In [45]: for key, value_iter in itertools.groupby('aaabbbaaccd'):
   ....:     print key, list(value_iter)
   ....:     
a ['a', 'a', 'a']
b ['b', 'b', 'b']
a ['a', 'a']
c ['c', 'c']
d ['d']
In [48]: data = ['a', 'bb', 'cc', 'ddd', 'eee', 'f']
In [49]: for key, value_iter in itertools.groupby(data, len):
   ....:     print key, list(value_iter)
   ....:     
1 ['a']
2 ['bb', 'cc']
3 ['ddd', 'eee']
1 ['f']
复制代码


注意,注意,注意:必须先排序后才能分组,因为 groupby 是通过比较相邻元素来分组的。可以看第二个例子,因为 a 和 f 没有排在一起,所以最后没有分组到同一个列表中。


itertools.islice


islice(iterable, [start,] stop [, step])
复制代码


切片选择,start 是开始索引,stop 是结束索引,step 是步长,start 和 step 可选。示例:


In [52]: list(itertools.islice([10, 6, 2, 8, 1, 3, 9], 5))
Out[52]: [10, 6, 2, 8, 1]
In [53]: list(itertools.islice(itertools.count(), 6))
Out[53]: [0, 1, 2, 3, 4, 5]
In [54]: list(itertools.islice(itertools.count(), 3, 10))
Out[54]: [3, 4, 5, 6, 7, 8, 9]
In [55]: list(itertools.islice(itertools.count(), 3, 10, 2))
Out[55]: [3, 5, 7, 9]
复制代码


itertools.tee


tee(iterable, n=2)
复制代码


从 iterable 创建 n 个独立的迭代器,以元组的形式返回。示例:


In [57]: itertools.tee("abcedf")
Out[57]: (<itertools.tee at 0x7fed7b8f59e0>, <itertools.tee at 0x7fed7b8f56c8>)
In [58]: iter1, iter2 = itertools.tee("abcedf")
In [59]: list(iter1)
Out[59]: ['a', 'b', 'c', 'e', 'd', 'f']
In [60]: list(iter2)
Out[60]: ['a', 'b', 'c', 'e', 'd', 'f']
In [61]: itertools.tee("abcedf", 3)
Out[61]:
(<itertools.tee at 0x7fed7b8f5cf8>,
 <itertools.tee at 0x7fed7b8f5cb0>,
 <itertools.tee at 0x7fed7b8f5b00>)


目录
相关文章
|
4月前
|
SQL 关系型数据库 数据库
Python SQLAlchemy模块:从入门到实战的数据库操作指南
免费提供Python+PyCharm编程环境,结合SQLAlchemy ORM框架详解数据库开发。涵盖连接配置、模型定义、CRUD操作、事务控制及Alembic迁移工具,以电商订单系统为例,深入讲解高并发场景下的性能优化与最佳实践,助你高效构建数据驱动应用。
589 7
|
4月前
|
监控 安全 程序员
Python日志模块配置:从print到logging的优雅升级指南
从 `print` 到 `logging` 是 Python 开发的必经之路。`print` 调试简单却难维护,日志混乱、无法分级、缺乏上下文;而 `logging` 支持级别控制、多输出、结构化记录,助力项目可维护性升级。本文详解痛点、优势、迁移方案与最佳实践,助你构建专业日志系统,让程序“有记忆”。
406 0
|
4月前
|
JSON 算法 API
Python中的json模块:从基础到进阶的实用指南
本文深入解析Python内置json模块的使用,涵盖序列化与反序列化核心函数、参数配置、中文处理、自定义对象转换及异常处理,并介绍性能优化与第三方库扩展,助你高效实现JSON数据交互。(238字)
499 4
|
开发者 Python
如何在Python中管理模块和包的依赖关系?
在实际开发中,通常会结合多种方法来管理模块和包的依赖关系,以确保项目的顺利进行和可维护性。同时,要及时更新和解决依赖冲突等问题,以保证代码的稳定性和可靠性
615 159
|
4月前
|
Java 调度 数据库
Python threading模块:多线程编程的实战指南
本文深入讲解Python多线程编程,涵盖threading模块的核心用法:线程创建、生命周期、同步机制(锁、信号量、条件变量)、线程通信(队列)、守护线程与线程池应用。结合实战案例,如多线程下载器,帮助开发者提升程序并发性能,适用于I/O密集型任务处理。
454 0
|
4月前
|
XML JSON 数据处理
超越JSON:Python结构化数据处理模块全解析
本文深入解析Python中12个核心数据处理模块,涵盖csv、pandas、pickle、shelve、struct、configparser、xml、numpy、array、sqlite3和msgpack,覆盖表格处理、序列化、配置管理、科学计算等六大场景,结合真实案例与决策树,助你高效应对各类数据挑战。(238字)
441 0
|
5月前
|
安全 大数据 程序员
Python operator模块的methodcaller:一行代码搞定对象方法调用的黑科技
`operator.methodcaller`是Python中处理对象方法调用的高效工具,替代冗长Lambda,提升代码可读性与性能。适用于数据过滤、排序、转换等场景,支持参数传递与链式调用,是函数式编程的隐藏利器。
197 4
|
5月前
|
存储 数据库 开发者
Python SQLite模块:轻量级数据库的实战指南
本文深入讲解Python内置sqlite3模块的实战应用,涵盖数据库连接、CRUD操作、事务管理、性能优化及高级特性,结合完整案例,助你快速掌握SQLite在小型项目中的高效使用,是Python开发者必备的轻量级数据库指南。
492 0
|
6月前
|
存储 安全 数据处理
Python 内置模块 collections 详解
`collections` 是 Python 内置模块,提供多种高效数据类型,如 `namedtuple`、`deque`、`Counter` 等,帮助开发者优化数据处理流程,提升代码可读性与性能,适用于复杂数据结构管理与高效操作场景。
444 0
|
7月前
|
数据安全/隐私保护 Python
抖音私信脚本app,协议私信群发工具,抖音python私信模块
这个实现包含三个主要模块:抖音私信核心功能类、辅助工具类和主程序入口。核心功能包括登录

推荐镜像

更多