python 之 itertools模块
官方:https://yiyibooks.cn/xx/python_352/library/itertools.html
参考:
https://blog.csdn.net/neweastsun/article/details/51965226
python 之 itertools模块
介绍
itertools是python内置的模块,使用简单且功能强大,使用只需简单一句导入:import itertools
chain()
与其名称意义一样,给它一个列表如 lists/tuples/iterables,链接在一起;返回iterables对象。
- letters = ['a', 'b', 'c', 'd', 'e', 'f']
- booleans = [1, 0, 1, 0, 0, 1]
- print(list(itertools.chain(letters,booleans)))
- # ['a', 'b', 'c', 'd', 'e', 'f', 1, 0, 1, 0, 0, 1]
- print(tuple(itertools.chain(letters,letters[3:])))
- # ('a', 'b', 'c', 'd', 'e', 'f', 'd', 'e', 'f')
- print(set(itertools.chain(letters,letters[3:])))
- # {'a', 'd', 'b', 'e', 'c', 'f'}
- print(list(itertools.chain(letters,letters[3:])))
- # ['a', 'b', 'c', 'd', 'e', 'f', 'd', 'e', 'f']
- for item in list(itertools.chain(letters,booleans)):
- print(item)
count()
生成无界限序列,count(start=0, step=1) ,示例从100开始,步长为2,循环10,打印对应值;必须手动break,count()会一直循环。
- i = 0
- for item in itertools.count(100,2):
- i += 1
- if i > 10 : break
- print(item)
filterfalse ()
Python filterfalse(contintion,data) 迭代过滤条件为false的数据(注意:最终结果仅会展示 为 False 的数据)。如果条件为空,返回data中为false的项;
- booleans = [1, 0, 1, 0, 0, 1]
- numbers = [23, 20, 44, 32, 7, 12]
- print(list(itertools.filterfalse(None,booleans)))
- # [0, 0, 0]
- print(list(itertools.filterfalse(lambda x : x < 20,numbers)))
- # [23, 20, 44, 32]
compress()
返回我们需要使用的元素,根据b集合中元素真值,返回a集中对应的元素。
- print(list(itertools.compress(letters,booleans)))
- # ['a', 'c', 'f']
starmap()
针对list中的每一项,调用函数功能。starmap(func,list[]) ;
- starmap(pow, [(2,5), (3,2), (10,3)]) --> 32 9 1000
- >>> from itertools import *
- >>> x = starmap(max,[[5,14,5],[2,34,6],[3,5,2]])
- >>> for i in x:
- >>> print (i)
- 14
- 34
- 5
repeat() 重复
repeat(object[, times]) 重复times次;
- repeat(10, 3) --> 10 10 10
dropwhile()
dropwhile(func, seq );当函数f执行返回假时, 开始迭代序列
- dropwhile(lambda x: x<5, [1,4,6,4,1]) --> 6 4 1
takewhile()
takewhile(predicate, iterable);返回序列,当predicate为true是截止。
- takewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4
islice()
islice(seq[, start], stop[, step]);返回序列seq的从start开始到stop结束的步长为step的元素的迭代器
- for i in islice("abcdef", 0, 4, 2):#a, c
- print i
矩阵大家族
product()
product(iter1,iter2, ... iterN, [repeat=1]);创建一个迭代器,生成表示item1,item2等中的项目的笛卡尔积的元组,repeat是一个关键字参数,指定重复生成序列的次数
- # product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
- # product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
- for i in product([1, 2, 3], [4, 5], [6, 7]):
- print i
- (1, 4, 6)
- (1, 4, 7)
- (1, 5, 6)
- (1, 5, 7)
- (2, 4, 6)
- (2, 4, 7)
- (2, 5, 6)
- (2, 5, 7)
- (3, 4, 6)
- (3, 4, 7)
- (3, 5, 6)
- (3, 5, 7)
permutations()
permutations(p[,r]);返回p中任意取r个元素做排列的元组的迭代器
- for i in permutations([1, 2, 3], 3):
- print i
- (1, 2, 3)
- (1, 3, 2)
- (2, 1, 3)
- (2, 3, 1)
- (3, 1, 2)
- (3, 2, 1)
combinations()
combinations(iterable,r);创建一个迭代器,返回iterable中所有长度为r的子序列,返回的子序列中的项按输入iterable中的顺序排序
note:不带重复
- for i in combinations([1, 2, 3], 2):
- print i
- (1, 2)
- (1, 3)
- (2, 3)
combinations_with_replacement() 同上, 带重复
例子:
- for i in combinations_with_replacement([1, 2, 3], 2):
- print i
- (1, 1)
- (1, 2)
- (1, 3)
- (2, 2)
- (2, 3)
- (3, 3)
应用示例
- # 求质数序列中1,3,5,7,9,11,13,15三个数之和为35的三个数;
- def get_three_data(data_list,amount):
- for data in list(itertools.combinations(data_list, 3)):
- if sum(data) == amount:
- print(data)
- #(7, 13, 15)
- #(9, 11, 15)
python 之 itertools模块的更多相关文章
- 【Python开发】Python:itertools模块
Python:itertools模块 itertools模块包含创建有效迭代器的函数,可以用各种方式对数据进行循环操作,此模块中的所有函数返回的迭代器都可以与for循环语句以及其他包含迭代器(如生成器 ...
- Python的itertools模块
本章将介绍Python自建模块itertools,更多内容请参考:Python参考指南 python的自建模块itertools提供了非常有用的用于操作迭代对象的函数. 首先,我们看看itertool ...
- Python中itertools模块
itertools模块包含创建有效迭代器的函数,可以用各种方式对数据进行循环操作,此模块中的所有函数返回的迭代器都可以与for循环语句以及其他包含迭代器(如生成器和生成器表达式)的函数联合使用. ch ...
- Python:itertools模块
itertools模块包含创建有效迭代器的函数,可以用各种方式对数据进行循环操作,此模块中的所有函数返回的迭代器都可以与for循环语句以及其他包含迭代器(如生成器和生成器表达式)的函数联合使用. ch ...
- python,itertools模块
itertools模块的使用: # coding=utf-8 """ itertools模块 """ import itertools im ...
- Python:itertools模块(转)
原文:http://www.cnblogs.com/cython/articles/2169009.html itertools模块包含很多创建迭代器的函数,可以用各种方式对数据进行循环操作,此模块中 ...
- 关于python的itertools模块
这是一个强大的模块 先来看一下它都有什么工具 无穷循环器 迭代器 参数 结果 ...
- Python学习笔记—itertools模块
这篇是看wklken的<Python进阶-Itertools模块小结> 学习itertools模块的学习笔记 在看itertools中各函数的源代码时,刚开始还比较轻松,但后面看起来就比较 ...
- 转:Python itertools模块
itertools Python的内建模块itertools提供了非常有用的用于操作迭代对象的函数. 首先,我们看看itertools提供的几个"无限"迭代器: >>& ...
随机推荐
- 使用stringstream类
当需要格式化int类型为字符串时,可以使用这个类, 需要包含这个文件头: #include <sstream> 然后这样使用: //打开保存进度的RPG文件. std::stringstr ...
- 余弦相似度及基于python的三种代码实现、与欧氏距离的区别
1.余弦相似度可用来计算两个向量的相似程度 对于如何计算两个向量的相似程度问题,可以把这它们想象成空间中的两条线段,都是从原点([0, 0, ...])出发,指向不同的方向.两条线段之间形成一个夹角, ...
- [Linux] sed命令使用之在文件中快速删除/增加指定行
1.删除文档的第一行 sed -i '1d' <file> 2.删除文档的最后一行sed -i '$d' <file> 3.在文档指定行中增加一行例如文档如下:echo &qu ...
- iOS runloop 自定义输入源
创建自定义输入源需要定义以下内容 1)输入源要处理的信息 2)使感兴趣的客户端知道如何和输入源交互的调度例程 3)处理其他任何客户发送请求的例程 4)使输入源失效的取消例程 上图的处理流程:主线程(M ...
- sockaddr和sockaddr_in的区别
struct sockaddr和struct sockaddr_in这两个结构体用来处理网络通信的地址. 在各种系统调用或者函数中,只要和网络地址打交道,就得用到这两个结构体. 网络中的地址包含3个方 ...
- THREE.OrbitControls参数控制
// Set to false to disable this control//鼠标控制是否可用 this.enabled = true; // "target" sets th ...
- ss client 配置
1.1安装ss apt-get install python-pippip install shadowsocks 1.2配置ss 新建一个配置文件config.json/etc/shadowsock ...
- 20179223《Linux内核原理与分析》第三周学习笔记
测试3的实验: 1. 用gcc -g编译vi输入的代码 2. 在main函数中设置一个行断点 3. 在main函数增加一个空循环,循环次数为自己学号后4位,设置一个约为学号一半的条件断点 4. 提交调 ...
- IIS项目发布完整流程
第一步: 发布项目 使在IIS上发布是防止源码泄露 第二步: 打开IIS(如果没有就自己安装一个)打开IIS步骤 控制面板>系统安全>管理工具>IIS管理器 填写网站的名称和之前发布 ...
- 让控制台支持 ANSI 转义序列,输出下划线、修改颜色或其他控制
各种操作系统的控制台都支持 ANSI 转义序列(ANSI Escape Code).使用转义序列,可以对控制台进行很多额外的定制,例如修改颜色.修改标题栏,将文字添加下划线等. 当然,.NET 已经帮 ...