这是一个强大的模块

先来看一下它都有什么工具

无穷循环器

迭代器         参数         结果                                                例子

count()     start, [step]   start, start+step, start+2*step, ...                count(10) --> 10 11 12 13 14 ...

cycle()     p               p0, p1, ... plast, p0, p1, ...                      cycle('ABCD') --> A B C D A B C D ...

repeat()    elem [,n]       elem, elem, elem, ... endlessly or up to n times    repeat(10, 3) --> 10 10 10

处理输入序列迭代器

迭代器          参数            结果                                        例子

chain()     p, q, ...           p0, p1, ... plast, q0, q1, ...              chain('ABC', 'DEF') --> A B C D E F

compress()  data, selectors     (d[0] if s[0]), (d[1] if s[1]), ...         compress('ABCDEF', [1,0,1,0,1,1]) --> A C E F

dropwhile() pred, seq           seq[n], seq[n+1], starting when pred fails  dropwhile(lambda x: x<5, [1,4,6,4,1]) --> 6 4 1

groupby()   iterable[, keyfunc] sub-iterators grouped by value of keyfunc(v)

ifilterfalse()  pred, seq       elements of seq where pred(elem) is False   ifilterfalse(lambda x: x%2, range(10)) --> 0 2 4 6 8

islice()    seq, [start,] stop [, step] elements from seq[start:stop:step]  islice('ABCDEFG', 2, None) --> C D E F G

imap()      func, p, q, ...     func(p0, q0), func(p1, q1), ...             imap(pow, (2,3,10), (5,2,3)) --> 32 9 1000

starmap()   func, seq           func(*seq[0]), func(*seq[1]), ...           starmap(pow, [(2,5), (3,2), (10,3)]) --> 32 9 1000

tee()       it, n               it1, it2 , ... itn splits one iterator into n

takewhile() pred, seq           seq[0], seq[1], until pred fails            takewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4

izip()      p, q, ...           (p[0], q[0]), (p[1], q[1]), ...             izip('ABCD', 'xy') --> Ax By

zip_longest  iter            

组合工具

迭代器          参数                        结果

product()       p, q, ... [repeat=1]        cartesian product, equivalent to a nested for-loop

permutations()  p[, r]                      r-length tuples, all possible orderings, no repeated elements

combinations()  p, r                        r-length tuples, in sorted order, no repeated elements

combinations_with_replacement() p, r        r-length tuples, in sorted order, with repeated elements

product('ABCD', repeat=2)                   AA AB AC AD BA BB BC BD CA CB CC CD DA DB DC DD

permutations('ABCD', 2)                     AB AC AD BA BC BD CA CB CD DA DB DC

combinations('ABCD', 2)                     AB AC AD BC BD CD

combinations_with_replacement('ABCD', 2)    AA AB AC AD BB BC BD CC CD DD

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

  1. python 之 itertools模块

    官方:https://yiyibooks.cn/xx/python_352/library/itertools.html 参考: https://blog.csdn.net/neweastsun/ar ...

  2. 【Python开发】Python:itertools模块

    Python:itertools模块 itertools模块包含创建有效迭代器的函数,可以用各种方式对数据进行循环操作,此模块中的所有函数返回的迭代器都可以与for循环语句以及其他包含迭代器(如生成器 ...

  3. Python的itertools模块

    本章将介绍Python自建模块itertools,更多内容请参考:Python参考指南 python的自建模块itertools提供了非常有用的用于操作迭代对象的函数. 首先,我们看看itertool ...

  4. Python中itertools模块

    itertools模块包含创建有效迭代器的函数,可以用各种方式对数据进行循环操作,此模块中的所有函数返回的迭代器都可以与for循环语句以及其他包含迭代器(如生成器和生成器表达式)的函数联合使用. ch ...

  5. Python:itertools模块

    itertools模块包含创建有效迭代器的函数,可以用各种方式对数据进行循环操作,此模块中的所有函数返回的迭代器都可以与for循环语句以及其他包含迭代器(如生成器和生成器表达式)的函数联合使用. ch ...

  6. python,itertools模块

    itertools模块的使用: # coding=utf-8 """ itertools模块 """ import itertools im ...

  7. Python:itertools模块(转)

    原文:http://www.cnblogs.com/cython/articles/2169009.html itertools模块包含很多创建迭代器的函数,可以用各种方式对数据进行循环操作,此模块中 ...

  8. Python学习笔记—itertools模块

    这篇是看wklken的<Python进阶-Itertools模块小结> 学习itertools模块的学习笔记 在看itertools中各函数的源代码时,刚开始还比较轻松,但后面看起来就比较 ...

  9. 转:Python itertools模块

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

随机推荐

  1. 四:Java使用google的thumbnailator工具对图片压缩水印等做处理

    Thumbnailator是一个非常好的图片开源工具 使用方法: 在pom中加入以下jar包 <!-- 图片缩略图 图片压缩 水印 start--> <dependency>& ...

  2. php 例子 如何转换ISO8601为 utc时间

    //firstpowertime "2017-01-02T13:22:22" 获取时间$firstpowertime=$list[$i]['firstpowertime'];//判 ...

  3. java环境变量和tomcat环境变量配置

    一.JAVA环境变量的配置1.首先下载JDK JDK可以在Oracle(甲骨文)公司的官方网站http://www.oracle.com下载2.安装完成后查看JDK安装路径一般是C:\Program ...

  4. open_basedir restriction in effect.文件访问没有权限异常

    本次搭建公司web开发环境遇到了不少坑首先认为可能是设置的目录写入权限问题,后面解决了发现不是 问题在于nginx配置文件进行了网站目录的保护 配置文件/usr/local/nginx/conf/fa ...

  5. 负载均衡手段之DNS轮询

    大多数域名注册商都支持对统一主机添加多条A记录,这就是DNS轮询,DNS服务器将解析请求按照A记录的顺序,随机分配到不同的IP上,这样就完成了简单的负载均衡.下图的例子是:有3台联通服务器.3台电信服 ...

  6. CCF-201412-3-集合竞价

    问题描述 试题编号: 201412-3 试题名称: 集合竞价 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 某股票交易所请你编写一个程序,根据开盘前客户提交的订单来确定某特定 ...

  7. mac下出现xcrun: error导致git、svn无法使用的解决办法

    现象:xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun ...

  8. memcached 安装以及在php中使用

    最近做的一个项目需要不断对数据库内容进行读取和刷新,所以数据库压力很大,用户数目多的时候响应速度也严重受影响. 于是准备将一部分常用数据通过缓存在服务器内存中,减少对数据库的操作来缓解压力(memca ...

  9. bootstrap validator 使用 带代码

    如何使用bootstrapVlidator插件? 下载bootstrapVlidator插件 在需要使用的页面引入bootstrapVlidator的js文件和css文件 如: 注: 在此基础之前必须 ...

  10. Atcoder R84 D Small Multiple

    题意:给定一个正整数K,求K的倍数中,各位上的数字之和最小是多少? 思路非常巧妙,对于一个数,我们有定义两种改变方式: 1.加1,则数字之和+1(9的情况另行考虑) 2.乘10,数字之和不变 对于末位 ...