Python 迭代器工具包【推荐】
0x01 介绍了迭代器的概念,即定义了 __iter__() 和 __next__() 方法的对象,或者通过 yield 简化定义的“可迭代对象”,而在一些函数式编程语言(见 0x02 Python 中的函数式编程)中,类似的迭代器常被用于产生特定格式的列表(或序列),这时的迭代器更像是一种数据结构而非函数(当然在一些函数式编程语言中,这两者并无本质差异)。Python 借鉴了 APL, Haskell, and SML 中的某些迭代器的构造方法,并在 itertools 中实现(该模块是通过 C 实现,源代码:/Modules/itertoolsmodule.c)。
itertools 模块提供了如下三类迭代器构建工具:
无限迭代
整合两序列迭代
组合生成器
1. 无限迭代
所谓无限(infinite)是指如果你通过 for...in... 的语法对其进行迭代,将陷入无限循环,包括:
count(start, [step]) cycle(p) repeat(elem [,n])
从名字大概可以猜出它们的用法,既然说是无限迭代,我们自然不会想要将其所有元素依次迭代取出,而通常是结合 map/zip 等方法,将其作为一个取之不尽的数据仓库,与有限长度的可迭代对象进行组合操作:
from itertools import cycle, count, repeat print(count.__doc__) count(start=0, step=1) --> count object Return a count object whose .__next__() method returns consecutive values. Equivalent to: def count(firstval=0, step=1): x = firstval while 1: yield x x += step counter = count() print(next(counter)) print(next(counter)) print(list(map(lambda x, y: x+y, range(10), counter))) odd_counter = map(lambda x: 'Odd#{}'.format(x), count(1, 2)) print(next(odd_counter)) print(next(odd_counter)) 0 1 [2, 4, 6, 8, 10, 12, 14, 16, 18, 20] Odd#1 Odd#3 print(cycle.__doc__) cycle(iterable) --> cycle object Return elements from the iterable until it is exhausted. Then repeat the sequence indefinitely. cyc = cycle(range(5)) print(list(zip(range(6), cyc))) print(next(cyc)) print(next(cyc)) [(0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (5, 0)] 1 2 print(repeat.__doc__) repeat(object [,times]) -> create an iterator which returns the object for the specified number of times. If not specified, returns the object endlessly. print(list(repeat('Py', 3))) rep = repeat('p') print(list(zip(rep, 'y'*3))) ['Py', 'Py', 'Py'] [('p', 'y'), ('p', 'y'), ('p', 'y')]
2. 整合两序列迭代
所谓整合两序列,是指以两个有限序列为输入,将其整合操作之后返回为一个迭代器,最为常见的 zip 函数就属于这一类别,只不过 zip 是内置函数。这一类别完整的方法包括:
accumulate() chain()/chain.from_iterable() compress() dropwhile()/filterfalse()/takewhile() groupby() islice() starmap() tee() zip_longest()
这里就不对所有的方法一一举例说明了,如果想要知道某个方法的用法,基本通过 print(method.__doc__) 就可以了解,毕竟 itertools 模块只是提供了一种快捷方式,并没有隐含什么深奥的算法。这里只对下面几个我觉得比较有趣的方法进行举例说明。
from itertools import cycle, compress, islice, takewhile, count # 这三个方法(如果使用恰当)可以限定无限迭代 # print(compress.__doc__) print(list(compress(cycle('PY'), [1, 0, 1, 0]))) # 像操作列表 l[start:stop:step] 一样操作其它序列 # print(islice.__doc__) print(list(islice(cycle('PY'), 0, 2))) # 限制版的 filter # print(takewhile.__doc__) print(list(takewhile(lambda x: x < 5, count()))) ['P', 'P'] ['P', 'Y'] [0, 1, 2, 3, 4] from itertools import groupby from operator import itemgetter print(groupby.__doc__) for k, g in groupby('AABBC'): print(k, list(g)) db = [dict(name='python', script=True), dict(name='c', script=False), dict(name='c++', script=False), dict(name='ruby', script=True)] keyfunc = itemgetter('script') db2 = sorted(db, key=keyfunc) # sorted by `script' for isScript, langs in groupby(db2, keyfunc): print(', '.join(map(itemgetter('name'), langs))) groupby(iterable[, keyfunc]) -> create an iterator which returns (key, sub-iterator) grouped by each value of key(value). A ['A', 'A'] B ['B', 'B'] C ['C'] c, c++ python, ruby from itertools import zip_longest # 内置函数 zip 以较短序列为基准进行合并, # zip_longest 则以最长序列为基准,并提供补足参数 fillvalue # Python 2.7 中名为 izip_longest print(list(zip_longest('ABCD', '123', fillvalue=0))) [('A', '1'), ('B', '2'), ('C', '3'), ('D', 0)]
3. 组合生成器
关于生成器的排列组合:
product(*iterables, repeat=1):两输入序列的笛卡尔乘积 permutations(iterable, r=None):对输入序列的完全排列组合 combinations(iterable, r):有序版的排列组合 combinations_with_replacement(iterable, r):有序版的笛卡尔乘积 from itertools import product, permutations, combinations, combinations_with_replacement print(list(product(range(2), range(2)))) print(list(product('AB', repeat=2))) [(0, 0), (0, 1), (1, 0), (1, 1)] [('A', 'A'), ('A', 'B'), ('B', 'A'), ('B', 'B')] print(list(combinations_with_replacement('AB', 2))) [('A', 'A'), ('A', 'B'), ('B', 'B')] # 赛马问题:4匹马前2名的排列组合(A^4_2) print(list(permutations('ABCDE', 2))) [('A', 'B'), ('A', 'C'), ('A', 'D'), ('A', 'E'), ('B', 'A'), ('B', 'C'), ('B', 'D'), ('B', 'E'), ('C', 'A'), ('C', 'B'), ('C', 'D'), ('C', 'E'), ('D', 'A'), ('D', 'B'), ('D', 'C'), ('D', 'E'), ('E', 'A'), ('E', 'B'), ('E', 'C'), ('E', 'D')] # 彩球问题:4种颜色的球任意抽出2个的颜色组合(C^4_2) print(list(combinations('ABCD', 2))) [('A', 'B'), ('A', 'C'), ('A', 'D'), ('B', 'C'), ('B', 'D'), ('C', 'D')]

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

PHP主要是过程式编程,但也支持面向对象编程(OOP);Python支持多种范式,包括OOP、函数式和过程式编程。PHP适合web开发,Python适用于多种应用,如数据分析和机器学习。

PHP适合网页开发和快速原型开发,Python适用于数据科学和机器学习。1.PHP用于动态网页开发,语法简单,适合快速开发。2.Python语法简洁,适用于多领域,库生态系统强大。

Python更适合初学者,学习曲线平缓,语法简洁;JavaScript适合前端开发,学习曲线较陡,语法灵活。1.Python语法直观,适用于数据科学和后端开发。2.JavaScript灵活,广泛用于前端和服务器端编程。

VS Code 可用于编写 Python,并提供许多功能,使其成为开发 Python 应用程序的理想工具。它允许用户:安装 Python 扩展,以获得代码补全、语法高亮和调试等功能。使用调试器逐步跟踪代码,查找和修复错误。集成 Git,进行版本控制。使用代码格式化工具,保持代码一致性。使用 Linting 工具,提前发现潜在问题。

VS Code 扩展存在恶意风险,例如隐藏恶意代码、利用漏洞、伪装成合法扩展。识别恶意扩展的方法包括:检查发布者、阅读评论、检查代码、谨慎安装。安全措施还包括:安全意识、良好习惯、定期更新和杀毒软件。

VS Code可以在Windows 8上运行,但体验可能不佳。首先确保系统已更新到最新补丁,然后下载与系统架构匹配的VS Code安装包,按照提示安装。安装后,注意某些扩展程序可能与Windows 8不兼容,需要寻找替代扩展或在虚拟机中使用更新的Windows系统。安装必要的扩展,检查是否正常工作。尽管VS Code在Windows 8上可行,但建议升级到更新的Windows系统以获得更好的开发体验和安全保障。

PHP起源于1994年,由RasmusLerdorf开发,最初用于跟踪网站访问者,逐渐演变为服务器端脚本语言,广泛应用于网页开发。Python由GuidovanRossum于1980年代末开发,1991年首次发布,强调代码可读性和简洁性,适用于科学计算、数据分析等领域。

在 VS Code 中,可以通过以下步骤在终端运行程序:准备代码和打开集成终端确保代码目录与终端工作目录一致根据编程语言选择运行命令(如 Python 的 python your_file_name.py)检查是否成功运行并解决错误利用调试器提升调试效率
