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. Item 29 优先考虑类型安全的异构容器

    集合API展示了泛型的一般用法.但是它们(Set,HashMap,Map)限制了每个容器只能有固定数目的类型参数.     比如Set集合,HashMap集合: import java.util.Ha ...

  2. 【BZOJ5010】【FJOI2017】矩阵填数 [状压DP]

    矩阵填数 Time Limit: 10 Sec  Memory Limit: 128 MB[Submit][Status][Discuss] Description 给定一个 h*w 的矩阵,矩阵的行 ...

  3. 【转载】Lua中实现类的原理

    原文地址 http://wuzhiwei.net/lua_make_class/ 不错,将metatable讲的很透彻,我终于懂了. --------------------------------- ...

  4. python学习笔记(二)之python简单实践

    1 安装python开发环境 Linux环境下自动安装好了python,可以通过以下命令更新到python最新版本. #echo "alias python=/usr/bin/python3 ...

  5. 超详细的Java面试题总结(二)之Java基础知识篇

    多线程和Java虚拟机 创建线程有几种不同的方式?你喜欢哪一种?为什么? 继承Thread类 实现Runnable接口 应用程序可以使用Executor框架来创建线程池 实现Callable接口. 我 ...

  6. javascript 事件知识集锦

    1.事件委托极其应用 转载的链接:  http://www.webhek.com/event-delegate/#comments 2. 解析javascript事件机制 转载链接:    http: ...

  7. 获取应用版本号,版本名称,包名,AppName,图标,是否是系统应用,获取手机中所有应用,所有进程

    PackageManager packageManager = getPackageManager(); PackageInfo packageInfo; = packageManager.getPa ...

  8. [device tree] interrupt mapping example

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

  9. monkey测试===Monkey测试结果分析(系列三)转

    Monkey测试结果分析 一. 初步分析方法: Monkey测试出现错误后,一般的差错步骤为以下几步: 1. 找到是monkey里面的哪个地方出错 2. 查看Monkey里面出错前的一些事件动作,并手 ...

  10. python基础===【字符串】所有相关操作

    #字符串的相关操作 #基本操作 #+ 字符串连接操作 str1 = '来是come走是go' str2 = '点头yes摇头no' result = str1 + str2 print(result) ...