python itertools的使用(转)
1. chain的使用
import itertools
listone = ['a','b','c']
listtwo = ['11','22','abc']
for item in itertools.chain(listone,listtwo):
print item
输出: a b c 11 22 abc
2. count的使用
i = 0
for item in itertools.count(100):
if i>10:
break
print item,
i = i+1
功能:从100开始数10个数,cout返回一个无界的迭代器,如果引入一个计数I,可以让它计数10次。。
输出:100 101 102 103 104 105 106 107 108 109 110
3.cycle的使用
import itertools
listone = ['a','b','c']
listtwo = ['11','22','abc']
for item in itertools.cycle(listone):
print item,
功能:从列表中取元素,到列表尾后再从头取...
无限循环,因为cycle生成的是一个无界的失代器
输出:a b c a b c a b c a b c a b c a b c a b c a b c a b c...
4.ifilter的使用,ifilter(fun,iterator)返回一个可以让fun返回True的迭代器
import itertools
def funLargeFive(x):
if x > 5:
return True
for item in itertools.ifilter(funLargeFive,range(-10,10)):
print item,
结果:6 7 8 9
5. imap的使用,imap(fun,iterator)返回一个迭代器,对iterator中的每个项目调用fun
import itertools
listthree = [1,2,3]
def funAddFive(x):
return x + 5
for item in itertools.imap(funAddFive,listthree):
print item,
返回:6 7 8 对listthree中的元素每个加了5后返回给迭代器
6.islice的使用,islice()(seq, [start,] stop [, step])
import itertools
listone = ['a','b','c']
listtwo = ['11','22','abc']
listthree = listone + listtwo
for item in itertools.islice(listthree,3,5):
print item,
功能:返回迭代器,其中的项目来自 将seq,从start开始,到stop结束,以step步长切割后
打印出:11 22
7.izip的使用,izip(*iterator)
import itertools
listone = ['a','b','c']
listtwo = ['11','22','abc']
listthree = listone + listtwo
for item in itertools.izip(listone,listtwo):
print item,
结果:('a', '11') ('b', '22') ('c', 'abc')
功能:返回迭代器,项目是元组,元组来自*iterator的组合
8. repeate,repeate(elem [,n])
import itertools
listone = ['a','b','c']
for item in itertools.repeat(listone,3):
print item,
结果:['a', 'b', 'c'] ['a', 'b', 'c'] ['a', 'b', 'c']
python itertools的使用(转)的更多相关文章
- 转:Python itertools模块
itertools Python的内建模块itertools提供了非常有用的用于操作迭代对象的函数. 首先,我们看看itertools提供的几个"无限"迭代器: >>& ...
- python, itertools模块
通过itertools模块,可以用各种方式对数据进行循环操作 1, chain() from intertools import chain for i in chain([1,2,3], ('a', ...
- python itertools 模块
Python的内建模块itertools提供了非常有用的用于操作迭代对象的函数 首先,我们看看itertools提供的几个“无限”迭代器: >>> import itertools ...
- 《笔记》Python itertools的groupby分组数据处理
今天遇到这么一个需求,需要将这样的数据进行分组处理: [(, ), (, ), (, ), (, ), (, ), (, )] 处理之后我可能需要得到这样的结果: [(, (, , (, , (, ) ...
- Python itertools模块详解
这货很强大, 必须掌握 文档 链接 http://docs.python.org/2/library/itertools.html pymotw 链接 http://pymotw.com/2/iter ...
- python itertools 模块讲解
1.介绍itertools 是python的迭代器模块,itertools提供的工具相当高效且节省内存. 使用这些工具,你将能够创建自己定制的迭代器用于高效率的循环. - 无限迭代器 itertool ...
- Python itertools/内置函数
https://docs.python.org/3.5/library/itertools.html#itertools.starmap // https://docs.python.org/3.5/ ...
- Python itertools.combinations 和 itertools.permutations 等价代码实现
最近编程时经常要用到排序组合的代码,想当年还抱着一些情况买了一本<组合数学>,不过现在这货也不知道被自己放哪里了,估计不会是垫桌子腿了吧. 由于去年去东北大学考博面试的时候遇到过可能涉及排 ...
- python itertools模块练习
参考 <python标准库> 也可以参考Vamei博客 列表用着很舒服,但迭代器不需要将所有数据同时存储在内存中. 本章练习一下python 标准库中itertools模块 合并 和 分解 ...
- [python] itertools库学习
最近做 cyber-dojo上的题,好几道都要用到排列组合.一开始我还老老实实自己写算法.后来一想,不对呀!python有那么多的库,为啥不用呢? 于是搜了下,发现这个:itertools 使用 he ...
随机推荐
- javascript拖拽原理与简单实现方法[demo]
美国人有一句常用的俗语—“Re-inventing the Wheel”,从字面上来解释就是“重新发明轮子”.可是轮子早已问世,再要去发明岂非劳而无功? 产品经理发下需求,实施者再到网上搜索代码,也许 ...
- html+js+node实现五子棋线上对战,五子棋最简易算法
首先附上我的github地址,https://github.com/jiangzhenfei/five,线上实例:http://47.93.103.19:5900/client/ 线上实例,你可以随意 ...
- 【Mysql优化】索引覆盖
索引覆盖 是指 如果查询的列恰好是索引的一部分,那么查询只需要在索引文件上进行,不需要回行到磁盘再找数据.这种查询速度非常快,称为”索引覆盖”,比平时的查询少一次到磁盘读数据的操作.(索引正好覆盖到查 ...
- 浅谈C语言中的强符号、弱符号、强引用和弱引用【转】
转自:http://www.jb51.net/article/56924.htm 首先我表示很悲剧,在看<程序员的自我修养--链接.装载与库>之前我竟不知道C有强符号.弱符号.强引用和弱引 ...
- 【swupdate文档 二】许可证
许可证 SWUpdate是免费软件.它的版权属于Stefano Babic和其他许多贡献代码的人(详情请参阅实际源代码和git提交信息). 您可以根据自由软件基金会发布的GNU通用公共许可证第2版的条 ...
- Linux内核的架构
GNU/Linux操作系统架构 备注:IPC进程间通.IPC(Inter-Process Communication)是共享"命名管道"的资源,它是为了让进程间通信而开放的命名管道 ...
- 设计模式之笔记--抽象工厂模式(Abstract Factory)
抽象工厂模式(Abstract Factory) 定义 抽象工厂模式(Abstract Factory),提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类. 类图 描述 多个抽象产品 ...
- 如何在苹果官网下载旧版本的Xcode
如何在苹果官网下载旧版本的Xcode 前段时间XcodeGhost事件让很多应用中招,不乏一些知名的互联网公司开发的应用.事件的起因是开发者使用了非官方的Xcode,这些Xcode带有xcodegho ...
- Struts2学习笔记02 之 使用
一.页面向Action传参 1.基本属性注入,页面命名name,action提供成员变量name并提供set方法. 2.域模型注入:页面用user.name对象点属性形式.action成员user对象 ...
- python math模块
import math math. ceil:取大于等于x的最小的整数值,如果x是一个整数,则返回x copysign:把y的正负号加到x前面,可以使用0 cos:求x的余弦,x必须是弧度 degre ...