通过itertools模块,可以用各种方式对数据进行循环操作

1, chain()

from intertools import chain

for i in chain([1,2,3], ('a', 'b', 'c'), 'abcde'):

print i

chain将可迭代的对象链接在一起,iter1循环完后,接着循环iter2.直到所有的iter循环完。

2, combinations()

from intertools import combinations

for i in combinations([1,2,3,4], 2):

print i

(1, 2)
(1, 3)
(1, 4)
(2, 3)
(2, 4)
(3, 4)

第一个参数是一个可迭代对象, 第二个参数是返回的长度。

3, dropwhile(predicate, iterable)

和fileter()的功能差不多

from intertools import dropwhile

x = lambda x : x < 5

for i in dropwhile(x, [1,2,3,4,5,6,7,8]):

print x

将iterable中不满足x的都扔掉了,只循环满足条件x的.

4, imap()

和Python 自带的map一样

eg:

from itertools import imap

x = lambda x, y : x + y

for i in imap(x, '1234', 'abcd'):

print i

1a

2b

3c

4d

x的两个参数分别来自于后面两个可迭代对象

for i in map(x, '1234', 'abcd'):

pirnt i

和上面结果一样

5,islice(iterable, start, end, step)

from itertools import islice

t = 'abcdefghijk'

for i in islice(t, 0, 5, 1):

print i

指循环s前5个元素。t[0], t[1], t[2], t[3], t[4]

for i in islice(t, 5):

print i

t之后只有一个参数,这个代表end. 如果想让这个代表start, 必须这样写: for i in islice(t, 5, None):表示从t[5]开始循环,一直到结束。步进默认为1.

6, repeat(object, times)   将object循环n次,不是循环object里面的元素,是循环它本身,不一定要是可迭代对象

from itertools import repeat

for i in repeat(5, 3):

print i

得到 5 5 5 (5是一个整数,所以不限于可迭代的对象,只是循环object本身,是什么就循环几次什么)

其他用法:izip(ite1,ite2) (等同于Python自带的zip),

freemao

FAFU

free_mao@qq.com

python, itertools模块的更多相关文章

  1. 转:Python itertools模块

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

  2. python itertools 模块

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

  3. python itertools 模块讲解

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

  4. python itertools模块练习

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

  5. Python itertools模块详解

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

  6. [转载] Python itertools模块详解

    原文在这里,写的很详细,感谢原作者,以下摘录要点. itertools用于高效循环的迭代函数集合. 无限迭代器 迭代器 参数 结果 例子 count() start, [step] start, st ...

  7. Python itertools模块中的product函数

    product 用于求多个可迭代对象的笛卡尔积(Cartesian Product),它跟嵌套的 for 循环等价.即: product(A, B) 和 ((x,y) for x in A for y ...

  8. python itertools模块实现排列组合

    转自:https://blog.csdn.net/specter11235/article/details/71189486 一.笛卡尔积:itertools.product(*iterables[, ...

  9. Python标准模块--itertools

    1 模块简介 Python提供了itertools模块,可以创建属于自己的迭代器.itertools提供的工具快速并且节约内存.开发者可以使用这些工具创建属于自己特定的迭代器,这些特定的迭代器可以用于 ...

随机推荐

  1. hibernate generator class=xxx id详解

    <id>元素中的<generator>用来为该持久化类的实例生成唯一的标识,hibernate提供了很多内置的实现.Increment:由hibernate自动递增生成标识符, ...

  2. 我们无法找到服务器加载工作簿的数据模型"的 SharePoint 网站,当您刷新 Excel 2013 工作簿中的数据透视表时出错

    假定您使用 Analysis Services 源在 Microsoft Excel 2013 中创建数据透视表.将 Excel 工作簿上载到 Microsoft SharePoint 网站中.当您尝 ...

  3. C#学习7.31判断体重是否超标

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  4. 如何删除 Ubuntu 上不再使用的旧内核

    提问:过去我已经在我的Ubuntu上升级了几次内核.现在我想要删除这些旧的内核镜像来节省我的磁盘空间.如何用最简单的方法删除Ubuntu上先前版本的内核? 在Ubuntu上,有几个方法来升级内核.在U ...

  5. ie8解决F12问题

    工作中,突然电脑上的ie8按F12掉不出来了,一直显示最小化.于是在网上找了很多方法,按这种方法可以解决问题. 1.cmd+r,输入regedit,调出 注册表编辑器. 2.HKEY_CURRENT_ ...

  6. 2.精通前端系列技术之seajs模块化使工作更简单(二)

    drag.js // JavaScript Document //B开发 define(function(require,exports,module){ function drag(obj){ ; ...

  7. ARM安装ROS- indigo

    Ubuntu ARM install of ROS Indigo 溪西创客小屋 There are currently builds of ROS for Ubuntu Trusty armhf. T ...

  8. Mac OS X 卸载MySQL

    sudo rm /usr/local/mysqlsudo rm -rf /usr/local/mysql*sudo rm -rf /Library/StartupItems/MySQLCOMsudo ...

  9. iOS开发中关于nslog的几种流行做法小结

    不管哪种方法,都必须在PCH文件中做下宏定义 DEBUG和RELEASE要分开,RELEASE时log打印要取消 方法一:简单直接,用几行代码搞定,简洁但功能少 #ifdef DEBUG #defin ...

  10. IOS弹出视图 LewPopupViewController

    LewPopupViewController是一款IOS弹出视图软件.iOS 下的弹出视图.支持iPhone/iPad. 软件截图 使用方法 弹出视图 1 2 3 4 5 PopupView *vie ...