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的使用(转)的更多相关文章

  1. 转:Python itertools模块

    itertools Python的内建模块itertools提供了非常有用的用于操作迭代对象的函数. 首先,我们看看itertools提供的几个"无限"迭代器: >>& ...

  2. python, itertools模块

    通过itertools模块,可以用各种方式对数据进行循环操作 1, chain() from intertools import chain for i in chain([1,2,3], ('a', ...

  3. python itertools 模块

    Python的内建模块itertools提供了非常有用的用于操作迭代对象的函数 首先,我们看看itertools提供的几个“无限”迭代器: >>> import itertools ...

  4. 《笔记》Python itertools的groupby分组数据处理

    今天遇到这么一个需求,需要将这样的数据进行分组处理: [(, ), (, ), (, ), (, ), (, ), (, )] 处理之后我可能需要得到这样的结果: [(, (, , (, , (, ) ...

  5. Python itertools模块详解

    这货很强大, 必须掌握 文档 链接 http://docs.python.org/2/library/itertools.html pymotw 链接 http://pymotw.com/2/iter ...

  6. python itertools 模块讲解

    1.介绍itertools 是python的迭代器模块,itertools提供的工具相当高效且节省内存. 使用这些工具,你将能够创建自己定制的迭代器用于高效率的循环. - 无限迭代器 itertool ...

  7. Python itertools/内置函数

    https://docs.python.org/3.5/library/itertools.html#itertools.starmap // https://docs.python.org/3.5/ ...

  8. Python itertools.combinations 和 itertools.permutations 等价代码实现

    最近编程时经常要用到排序组合的代码,想当年还抱着一些情况买了一本<组合数学>,不过现在这货也不知道被自己放哪里了,估计不会是垫桌子腿了吧. 由于去年去东北大学考博面试的时候遇到过可能涉及排 ...

  9. python itertools模块练习

    参考 <python标准库> 也可以参考Vamei博客 列表用着很舒服,但迭代器不需要将所有数据同时存储在内存中. 本章练习一下python 标准库中itertools模块 合并 和 分解 ...

  10. [python] itertools库学习

    最近做 cyber-dojo上的题,好几道都要用到排列组合.一开始我还老老实实自己写算法.后来一想,不对呀!python有那么多的库,为啥不用呢? 于是搜了下,发现这个:itertools 使用 he ...

随机推荐

  1. 【lydsy1407】拓展欧几里得求解不定方程+同余方程

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1407 题意: 有n个野人,野人各自住在第c[i]个山洞中(山洞成环状),每年向前走p[i] ...

  2. cocos2dx 某缩放的页面 CCTableView最后一个标签无法点中

    有一个二级界面,在ipad4下面放大到1.6倍,直接对最外层的CCLayer缩放的,里面包含有CCTableView.结果运行的时候无法选中到最后一个标签,无论总的标签是2个还是更多,单步调试,发现到 ...

  3. Coursera在线学习---第八节.K-means聚类算法与主成分分析(PCA)

    一.K-means聚类中心初始化问题. 1)随机初始化各个簇类的中心,进行迭代,直到收敛,并计算代价函数J. 如果k=2~10,可以进行上述步骤100次,并分别计算代价函数J,选取J值最小的一种聚类情 ...

  4. 【转】MP3文件原理及结构解析

    1.引言文件压缩技术的日新月异使得MP3成为时下最烫手的音乐格式,优质的音乐随着0与1的排列迅速散布 到世界各地,撼动人心.何谓MP3?MP3的全称是MPEG Audio Layer 3,它是一种高效 ...

  5. 转 一次完整地http请求

    作者:斯巴达克斯 时间:January 11, 2014 分类:WEB 声明:本文章中的说法仅是个人理解总结,不一定完全正确,但是可以有助于理解. 关于HTTP协议可以参考以下: HTTP协议漫谈 h ...

  6. [device tree] interrupt mapping example

    This is for Devicetree Specification Release 0.1 Interrupt Mapping Example p19 在講解前,先帶進一些 PCI 的基礎觀念 ...

  7. 安全测试===Web 安全渗透方面的学习路线

    作者:向生李链接:https://www.zhihu.com/question/21914899/answer/39344435来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...

  8. sublime Text3 === 无法输入input的问题解决办法

    sublimetext无法对input或者raw_input执行.因此搜了很多方法后,解决了这个问题: 1.先下载插件sublimerepl ,如果无法下载,请点击https://github.com ...

  9. 安全测试===黑客攻击常用cmd命令大全

    黑客常用命令大全net user heibai lovechina /add 加一个heibai的用户密码为lovechina net localgroup Administrators heibai ...

  10. 20180104 wdcp中的mysql重启不成功

    1.重启不成功是由于/www/wdlinux/mysql-5.5.54/data 中的ib_logfile0.ib_logfile1 和ibdata1的文件存在,可用netstat -lnpt查看当前 ...