Python itertools 部分用法
过滤的部分用法
import itertoolsdef vowel(c):return c.lower() in 'aeiou'name = 'Aardvark'print(list(filter(vowel, 'Aardvark')))
# ['A', 'a', 'a']# 与 filter 函数的作用类似,不过 predicate 的 逻辑是相反的:predicate 返回假值时产出对应 的元素
print(list(itertools.filterfalse(vowel, 'Aardvark')))
# ['r', 'd', 'v', 'r', 'k']# 处理 it,跳过 predicate 的计算结果为真值的元 素,然后产出剩下的各个元素(不再进一步检 查)满足条件的值都会丢弃直到有元素不满足为止
print(list(itertools.dropwhile(vowel, 'Aardvark')))
# ['r', 'd', 'v', 'a', 'r', 'k']# predicate 返回真值时产出对应的元素,然后立 即停止,不再继续检查
print(list(itertools.takewhile(vowel, 'Aardvark')))
# ['A', 'a']# 并行处理两个可迭代的对象;如果 selector_it 中的元素是真值,产出 it 中对应的元素
print(list(itertools.compress('Aardvark', (1, 0, 1, 1, 0, 1))))
# ['A', 'r', 'd', 'a']# 产出 it 的切片,作用类似于 s[:stop] 或 s[start:stop:step],不过 it 可以是任何可迭代 的对象,而且这个函数实现的是惰性操作
print(list(itertools.islice('Aardvark', 4)))
# ['A', 'a', 'r', 'd']
用于映射的生成器 accumulate
import itertoolsimport operatorsample = [5, 4, 2, 8, 7, 6, 3, 0, 9, 1]# 计算总和
print(list(itertools.accumulate(sample)))
# [5, 9, 11, 19, 26, 32, 35, 35, 44, 45]# 计算最小值
print(list(itertools.accumulate(sample, min)))
# [5, 4, 2, 2, 2, 2, 2, 0, 0, 0]# 计算最大值
print(list(itertools.accumulate(sample, max)))
# [5, 5, 5, 8, 8, 8, 8, 8, 9, 9]# 计算乘积
print(list(itertools.accumulate(sample, operator.mul)))
# [5, 20, 40, 320, 2240, 13440, 40320, 0, 0, 0]# 计算阶乘
print(list(itertools.accumulate(range(1, 11), operator.mul)))
# [1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800]# 从1 开始,为单词中的字母编号
print(list(enumerate('albatroz', 1)))
# [(1, 'a'), (2, 'l'), (3, 'b'), (4, 'a'), (5, 't'), (6, 'r'), (7, 'o'), (8, 'z')]# 从 0 到 10,计算各个整数的平方
print(list(map(operator.mul, range(11), range(11))))
# [0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]# 计算两个可迭代对象中对应位置上的两个元素之积,元素最少的那 个可迭代对象到头后就停止
print(list(map(operator.mul, range(11), [2, 4, 8])))
# [0, 4, 16]# 作用等同于内置的 zip 函数
print(list(map(lambda a, b: (a, b), range(11), [2, 4, 8])))
# [(0, 2), (1, 4), (2, 8)]# 从 1 开始,根据字母所在的位置,把字母重复相应的次数
print(list(itertools.starmap(operator.mul, enumerate('albatroz', 1))))
# ['a', 'll', 'bbb', 'aaaa', 'ttttt', 'rrrrrr', 'ooooooo', 'zzzzzzzz']# [5, 4, 2, 8, 7, 6, 3, 0, 9, 1]
print(list(itertools.accumulate(sample)))
# [5, 9, 11, 19, 26, 32, 35, 35, 44, 45]
# 计算平均值
print(list(itertools.starmap(lambda a, b: b / a, enumerate(itertools.accumulate(sample), 1))))
# [5.0, 4.5, 3.6666666666666665, 4.75, 5.2, 5.333333333333333, 5.0, 4.375, 4.888888888888889, 4.5]
用于合并的生成器函数 chain
import itertoolsimport operator# 调用 chain 函数时通常传入两个或更多个可迭代对象。 用于连接多个列表或者迭代器。
print(list(itertools.chain('ABC', range(2))))
# ['A', 'B', 'C', 0, 1]# 如果只传入一个可迭代的对象,那么 chain 函数没什么用
print(list(itertools.chain(enumerate('ABC'))))
# [(0, 'A'), (1, 'B'), (2, 'C')]# 但是 chain.from_iterable 函数从可迭代的对象中获取每个元素, 然后按顺序把元素连接起来,前提是各个元素本身也是可迭代的对象
print(list(itertools.chain.from_iterable(enumerate('ABC'))))
# [0, 'A', 1, 'B', 2, 'C']# zip 常用于把两个可迭代的对象合并成一系列由两个元素组成的元组
print(list(zip('ABC', range(5))))
# [('A', 0), ('B', 1), ('C', 2)]# zip 可以并行处理任意数量个可迭代的对象,不过只要有一个可迭代 的对象到头了,生成器就停止
print(list(zip('ABC', range(5), [10, 20, 30, 40])))
# [('A', 0, 10), ('B', 1, 20), ('C', 2, 30)]# itertools.zip_longest 函数的作用与 zip 类似,不过输入的所有 可迭代对象都会处理到头,如果需要会填充 None
print(list(itertools.zip_longest('ABC', range(5))))
# [('A', 0), ('B', 1), ('C', 2), (None, 3), (None, 4)]# fillvalue 关键字参数用于指定填充的值
print(list(itertools.zip_longest('ABC', range(5), fillvalue='?')))
# [('A', 0), ('B', 1), ('C', 2), ('?', 3), ('?', 4)]
product 生成器函数
import itertools# 三个字符的字符串与两个整数的值域得到的笛卡儿积是六个元组 (因为 3 * 2 等于 6)
print(list(itertools.product('ABC', range(2))))
# [('A', 0), ('A', 1), ('B', 0), ('B', 1), ('C', 0), ('C', 1)]suits = 'spades hearts diamonds clubs'.split()# 两张牌('AK')与四种花色得到的笛卡儿积是八个元组
print(list(itertools.product('AK', suits)))
# [('A', 'spades'), ('A', 'hearts'), ('A', 'diamonds'), ('A', 'clubs'), ('K', 'spades'), ('K', 'hearts'), ('K', 'diamonds'), ('K', 'clubs')]# 如果传入一个可迭代的对象,product 函数产出的是一系列只有一 个元素的元组,不是特别有用
print(list(itertools.product('ABC')))
# [('A',), ('B',), ('C',)# repeat=N 关键字参数告诉 product 函数重复 N 次处理输入的各个 可迭代对象
print(list(itertools.product('ABC', repeat=2)))
# [('A', 'A'), ('A', 'B'), ('A', 'C'), ('B', 'A'), ('B', 'B'), ('B', 'C'), ('C', 'A'), ('C', 'B'), ('C', 'C')]print(list(itertools.product(range(2), repeat=3)))
# [(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1), (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1)]rows = itertools.product('AB', range(2), repeat=2)
for row in rows: print(row)
# ('A', 0, 'A', 0)
# ('A', 0, 'A', 1)
# ('A', 0, 'B', 0)
# ('A', 0, 'B', 1)
# ('A', 1, 'A', 0)
# ('A', 1, 'A', 1)
# ('A', 1, 'B', 0)
# ('A', 1, 'B', 1)
# ('B', 0, 'A', 0)
# ('B', 0, 'A', 1)
# ('B', 0, 'B', 0)
# ('B', 0, 'B', 1)
# ('B', 1, 'A', 0)
# ('B', 1, 'A', 1)
# ('B', 1, 'B', 0)
# ('B', 1, 'B', 1)
count、repeat、cycle 用法
import itertoolsimport operator# 使用 count 函数构建 ct 生成器
ct = itertools.count()# 获取 ct 中的第一个元素
print(next(ct))
# 0# 不能使用 ct 构建列表,因为 ct 是无穷的,所以我获取接下来的 3 个元素。
print(next(ct), next(ct), next(ct))
# 1 2 3# 如果使用 islice 或 takewhile 函数做了限制,可以从 count 生成 器中构建列表
print(list(itertools.islice(itertools.count(1, .3), 3)))
# [1, 1.3, 1.6]# 使用 'ABC' 构建一个 cycle 生成器,然后获取第一个元素 ——'A'
cy = itertools.cycle('ABC')
print(next(cy))
# A# 只有受到 islice 函数的限制,才能构建列表;这里获取接下来的 7 个元素
print(list(itertools.islice(cy, 7)))
# ['B', 'C', 'A', 'B', 'C', 'A', 'B']# 构建一个 repeat 生成器,始终产出数字 7
rp = itertools.repeat(7)
print(next(rp), next(rp))
# 7 7# 传入 times 参数可以限制 repeat 生成器生成的元素数量:这里会 生成 4 次数字 8
print(itertools.repeat(8, 4))
# repeat(8, 4)# repeat 函数的常见用途:为 map 函数提供固定参数,这里提供的是 乘数 5。
print(list(map(operator.mul, range(11), itertools.repeat(5))))
# [0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50]
Python itertools 部分用法相关推荐
- Python Itertools.chain()用法【将一组迭代对象串联起来,形成一个更大的迭代器】
它是一个需要一系列可迭代对象并返回一个可迭代对象的函数.它将所有可迭代对象组合在一起,并生成一个可迭代对象作为输出. 场景一: chain()可以把一组迭代对象串联起来,形成一个更大的迭代器: &g ...
- 【python日用】itertools.product用法
标准语法 itertools.product(*iterables[, repeat]) 含义 Cartesian product of input iterables. Roughly equiva ...
- python中itertools的用法,【python日用】itertools.product用法
标准语法 itertools.product(*iterables[, repeat]) 含义 Cartesian product of input iterables. Roughly equiva ...
- Python itertools 模块
Python itertools 本模块标准化了一个快速.高效利用内存的核心工具集.它们一起形成了"迭代器代数",这使得在纯 Python 中有可能创建简洁又高效的专用工具. 无穷 ...
- python itertools详解及使用示例
目录 1. 前言 2. Infinite iterators 2.1 count(start=0, step=1) 2.2 cycle(iterable) 2.3 repeat(object[, ti ...
- Python| itertools之pairwise:获取连续的重叠对
简介 最近刷力扣好几次都遇到了pairwise这个函数,感觉还挺方便的,今天又遇到后,搜索了一下他的含义及其用法. 总结如下. 参考网址: 「Python」Python 标准库之 itertools ...
- 简单介绍Python中异常处理用法
这篇文章主要给大家分享的是 Python中异常处理用法,为了保证程序的健壮性与容错性,即在遇到错误时候程序不会崩溃,我们需要对异常进行处理,下面来看看文章对此的用法,需要的朋友可以参考一下 为了保证程 ...
- python的继承用法_【后端开发】python中继承有什么用法?python继承的用法详解
本篇文章给大家带来的内容是关于python中继承有什么用法?python继承的用法详解,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 面向对象三大特征 1.封装:根据职责将属性和方法 ...
- python matplotlib 简单用法
python matplotlib 简单用法 具体内容请参考官网 代码 import matplotlib.pyplot as plt import numpy as np # 支持中文 plt.rc ...
最新文章
- HDOJ 1914 The Stable Marriage Problem
- python图像识别步骤_利用百度智能云结合Python体验图像识别(转载来自qylruirui)
- vue打包路径.html问题,如何解决vue.js打包报错问题
- 顶层 pom 中的 dependencies 与 dependencyManagement 区别
- 深度学习会不会被取代?深度学习必看发展史
- tomcat不重启java文件自动编译
- bzoj2242 [SDOI2011]计算器 exgcd+ksm+bsgs
- Mysql字符串组合的问题
- java监听器的原理与实现
- scrollview嵌套listview 滑动事件冲突的解决方法
- cmk聪明客机器人餐厅_住了10年的老房大改造,45客餐厅全开放,更有整面大衣柜,高级又实用!...
- Linux 查看网络速率
- iOS 各种项目源码集
- Chrome使用、插件、脚本
- 马尔可夫不等式、切比雪夫不等式、柯西-施瓦茨不等式
- 让你5分钟明白美国金融危机爆发的原因!
- 电子产品--耐久性试验--可靠性试验--热测试试验--老化试验
- 微信个人赞赏码怎么用?微信赞赏码使用教程详解
- c语言 srand was not,c语言srand生成随机数的问题
- js如何取到ajax回调函数值,js怎样获取调用回调函数的参数值