该库为满足特定需要的比较高效的迭代器内置库,在数据科学中的应用也不少,故有必要了解一下:

import itertools
import sys

无限迭代器(Infinite iterators)

Iterator Arguments Results Example
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
  • itertools.count
a=itertools.count(5)
type(a)
itertools.count
next(a)
5
next(a)
6
next(a)
7
b=itertools.count(6,2)
next(b)
6
next(b)
8
  • itertools.cycle
a=itertools.cycle([1,2])
next(a)
1
next(a)
2
next(a)
1
next(a)
2
b=itertools.cycle('ABC')
next(b)
'A'
next(b)
'B'
next(b)
'C'
next(b)
'A'
  • itertools.repeat
a=itertools.repeat([1,2,3])
next(a)
[1, 2, 3]
next(a)
[1, 2, 3]
b=itertools.repeat('ABC',3)
next(b)
'ABC'
next(b)
'ABC'
next(b)
'ABC'
try:
next(b)
except Exception as e:
print(sys.exc_info())
(<class 'StopIteration'>, StopIteration(), <traceback object at 0x0000024CB3EE2E08>)

最短输入停止迭代器(Iterators terminating on the shortest input sequence)

Iterator Arguments Results Example
accumulate() p [,func] p0, p0+p1, p0+p1+p2, … accumulate([1,2,3,4,5]) --> 1 3 6 10 15
chain() p, q, … p0, p1, … plast, q0, q1, … chain('ABC', 'DEF') --> A B C D E F
chain.from_iterable() iterable p0, p1, … plast, q0, q1, … chain.from_iterable(['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
filterfalse() pred, seq elements of seq where pred(elem) is false filterfalse(lambda x: x%2, range(10)) --> 0 2 4 6 8
groupby() iterable[, key] sub-iterators grouped by value of key(v)
islice() seq, [start,] stop [, step] elements from seq[start:stop:step] islice('ABCDEFG', 2, None) --> C D E F G
starmap() func, seq func(seq[0]), func(seq[1]), … starmap(pow, [(2,5), (3,2), (10,3)]) --> 32 9 1000
takewhile() pred, seq seq[0], seq[1], until pred fails takewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4
tee() it, n it1, it2, … itn splits one iterator into n
zip_longest() p, q, … (p[0], q[0]), (p[1], q[1]), … zip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D-
  • itertools.accumulate
a=itertools.accumulate([1,2,3,4])
list(a)
[1, 3, 6, 10]
b=itertools.accumulate('abc')
list(b)
['a', 'ab', 'abc']
  • itertools.chain
a=itertools.chain([1,2],[3,4],[5,6])
list(a)
[1, 2, 3, 4, 5, 6]
b=itertools.chain('ab','cd')
list(b)
['a', 'b', 'c', 'd']
  • itertools.chain.from_iterable
a=itertools.chain.from_iterable(['abc','def'])
list(a)
['a', 'b', 'c', 'd', 'e', 'f']
  • itertools.compress
a=itertools.compress('abcdef',[1,0,1,0,1,1])
list(a)
['a', 'c', 'e', 'f']
a=itertools.compress('abcdef',[1,False,1,False,1,0])
list(a)
['a', 'c', 'e']
  • itertools.dropwhile
a=itertools.dropwhile(lambda x:x<5,[1,4,6,4,1])
list(a)
[6, 4, 1]
  • itertools.takewhile
a=itertools.takewhile(lambda x:x<5,[1,4,6,4,1])
list(a)
[1, 4]
  • filterfalse
a=itertools.filterfalse(lambda x:x%2,range(10))
list(a)
[0, 2, 4, 6, 8]
  • groupby
x = [(1, 2), (2, 3), (1, 4), (5, 5), (3, 4), (2, 6)]
a=itertools.groupby(x,lambda x:x[0])
for i in a:
print(i[0],list(i[1]))
1 [(1, 2)]
2 [(2, 3)]
1 [(1, 4)]
5 [(5, 5)]
3 [(3, 4)]
2 [(2, 6)]
  • itertools.islice
a=itertools.islice('abcdefg',2,None)
list(a)
['c', 'd', 'e', 'f', 'g']
b=itertools.islice('abcdefg',2,4)
list(b)
['c', 'd']
  • itertools.starmap
a=itertools.starmap(pow,[(2,5),(3,2)])
list(a)
[32, 9]
  • itertools.tee
a=itertools.tee([1,2,3],2)
list(a)
[<itertools._tee at 0x24cb3f29448>, <itertools._tee at 0x24cb3f29888>]
a=itertools.tee([1,2,3],2)
list(a[0])
[1, 2, 3]
list(a[1])
[1, 2, 3]
  • itertools.zip_longest
a=itertools.zip_longest('abcd','xy',fillvalue='-')
list(a)
[('a', 'x'), ('b', 'y'), ('c', '-'), ('d', '-')]

组合迭代器

Iterator Arguments Results
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
  • itertools.product
a=itertools.product([1,2,3],[4,5,6])
list(a)
[(1, 4), (1, 5), (1, 6), (2, 4), (2, 5), (2, 6), (3, 4), (3, 5), (3, 6)]
a=itertools.product([1,2,3],[4,5,6],repeat=2)
list(a)
[(1, 4, 1, 4),
(1, 4, 1, 5),
(1, 4, 1, 6),
(1, 4, 2, 4),
(1, 4, 2, 5),
(1, 4, 2, 6),
(1, 4, 3, 4),
(1, 4, 3, 5),
(1, 4, 3, 6),
(1, 5, 1, 4),
(1, 5, 1, 5),
(1, 5, 1, 6),
(1, 5, 2, 4),
(1, 5, 2, 5),
(1, 5, 2, 6),
(1, 5, 3, 4),
(1, 5, 3, 5),
(1, 5, 3, 6),
(1, 6, 1, 4),
(1, 6, 1, 5),
(1, 6, 1, 6),
(1, 6, 2, 4),
(1, 6, 2, 5),
(1, 6, 2, 6),
(1, 6, 3, 4),
(1, 6, 3, 5),
(1, 6, 3, 6),
(2, 4, 1, 4),
(2, 4, 1, 5),
(2, 4, 1, 6),
(2, 4, 2, 4),
(2, 4, 2, 5),
(2, 4, 2, 6),
(2, 4, 3, 4),
(2, 4, 3, 5),
(2, 4, 3, 6),
(2, 5, 1, 4),
(2, 5, 1, 5),
(2, 5, 1, 6),
(2, 5, 2, 4),
(2, 5, 2, 5),
(2, 5, 2, 6),
(2, 5, 3, 4),
(2, 5, 3, 5),
(2, 5, 3, 6),
(2, 6, 1, 4),
(2, 6, 1, 5),
(2, 6, 1, 6),
(2, 6, 2, 4),
(2, 6, 2, 5),
(2, 6, 2, 6),
(2, 6, 3, 4),
(2, 6, 3, 5),
(2, 6, 3, 6),
(3, 4, 1, 4),
(3, 4, 1, 5),
(3, 4, 1, 6),
(3, 4, 2, 4),
(3, 4, 2, 5),
(3, 4, 2, 6),
(3, 4, 3, 4),
(3, 4, 3, 5),
(3, 4, 3, 6),
(3, 5, 1, 4),
(3, 5, 1, 5),
(3, 5, 1, 6),
(3, 5, 2, 4),
(3, 5, 2, 5),
(3, 5, 2, 6),
(3, 5, 3, 4),
(3, 5, 3, 5),
(3, 5, 3, 6),
(3, 6, 1, 4),
(3, 6, 1, 5),
(3, 6, 1, 6),
(3, 6, 2, 4),
(3, 6, 2, 5),
(3, 6, 2, 6),
(3, 6, 3, 4),
(3, 6, 3, 5),
(3, 6, 3, 6)]
  • itertools.permutations
for i in itertools.permutations([1,2,3],2):
print (i)
(1, 2)
(1, 3)
(2, 1)
(2, 3)
(3, 1)
(3, 2)
for i in itertools.permutations('123',2):
print (i)
('1', '2')
('1', '3')
('2', '1')
('2', '3')
('3', '1')
('3', '2')
for i in itertools.permutations('122',2):
print (i)
('1', '2')
('1', '2')
('2', '1')
('2', '2')
('2', '1')
('2', '2')
  • itertools.combinations
a=itertools.combinations([1,2,3],2)
list(a)
[(1, 2), (1, 3), (2, 3)]
a=itertools.combinations('abcd',2)
list(a)
[('a', 'b'), ('a', 'c'), ('a', 'd'), ('b', 'c'), ('b', 'd'), ('c', 'd')]
  • itertools.combinations_with_replacement
a=itertools.combinations_with_replacement('abcd',2)
list(a)
[('a', 'a'),
('a', 'b'),
('a', 'c'),
('a', 'd'),
('b', 'b'),
('b', 'c'),
('b', 'd'),
('c', 'c'),
('c', 'd'),
('d', 'd')]

Python内置库itertools简单学习的更多相关文章

  1. python内置函数的简单使用和介绍

    """内置函数的简单使用和介绍参考链接:https://docs.python.org/3/library/functions.html ""&quo ...

  2. Python 内置库 sys用法

    sys模块功能众多,这边先学习几个常用的方法sys常见函数列表① sys.argv: 实现从程序外部向程序传递参数.其实sys.argv[]就是一个列表,里面的项为用户输入的参数,但是sys.argv ...

  3. 使用python内置库pytesseract实现图片验证码的识别

    环境准备: 1.安装Tesseract模块 git文档地址:https://digi.bib.uni-mannheim.de/tesseract/ 下载后就是一个exe安装包,直接右击安装即可,安装完 ...

  4. 使用python内置库matplotlib,实现折线图的绘制

    环境准备: 需要安装matplotlib,安装方式: pip install matplotlib 直接贴代码喽: #引入模块 from matplotlib import pyplot,font_m ...

  5. Python学习:6.python内置函数

    Python内置函数 python内置函数,是随着python解释器运行而创建的函数,不需要重新定义,可以直接调用,那python的内置函数有哪些呢,接下来我们就了解一下python的内置函数,这些内 ...

  6. python算法常用技巧与内置库

    python算法常用技巧与内置库 近些年随着python的越来越火,python也渐渐成为了很多程序员的喜爱.许多程序员已经开始使用python作为第一语言来刷题. 最近我在用python刷题的时候想 ...

  7. 常用的python的内置库或者第三方库

    内置库:re,json,time,random,sys,os, 第三方库:转载: https://www.cnblogs.com/jiangchunsheng/p/9275881.htmlReques ...

  8. python内置函数简单归纳

    做python小项目的时候发现熟练运用python内置函数,可以节省很多的时间,在这里整理一下,便于以后学习或者工作的时候查看.函数的参数可以在pycharm中ctrl+p查看. 1.abs(x):返 ...

  9. RF内置库-----内置库的学习过程总结

    前段时间充忙的学习RF,系统学习完之后就开始动手做各种接口的测试,虽然各类的接口测试基本能跑通了,但是重复造车的问题存在太明显.RF本身内置库就已经比较丰富,比如不需要import直接就加载到内存的B ...

  10. 几个可以提高工作效率的Python内置小工具

    在这篇文章里,我们将会介绍4个Python解释器自身提供的小工具.这些小工具在笔者的日常工作中经常用到,减少了各种时间的浪费,然而,却很容易被大家忽略.每当有新来的同事看到我这么使用时,都忍不住感叹, ...

随机推荐

  1. typecho引入五秒盾,缓解服务器压力

    功能是:对访客的访问频率会先一步判断,根据用户自定义的范围,将频率过高的访客跳转向127.0.0.1,而没有达到频率的访客则会进行cookies验证,这样更大程序的对恶意流量攻击进行拦截,并且有效缓解 ...

  2. Kubernetes - [03] 安装部署

    Kubeadm 部署k8s集群 一.准备工作 1.1.组件 组件:Harbor(私有Docker Hub).Router 服务器操作系统:Centos7 +(内核3.0+,最好内核4.40+) 1.2 ...

  3. 记录:tinyrenderer

    Bresenham's line drawing(布雷森汉姆算法) 进行games101的光栅化作业时,对其渲染原理仍不甚了解,找到tinyrenderer软光栅项目.在此记录下试错的过程. 作者在最 ...

  4. 寻找旋转排序数组中的最小值 II

    地址:https://leetcode-cn.com/problems/find-minimum-in-rotated-sorted-array-ii/ <?php /** 154. 寻找旋转排 ...

  5. 【插件介绍】Mesh2Geom插件

    Mesh to Geometry Plugin,来自达索官方论坛社区 原帖链接:Mesh to Geometry Plugin plugin feature: 允许Abaqus 用户从网格文件生成几何 ...

  6. TypeError: 'NoneType' object is not iterable 一次错误场景

    TypeError: 'NoneType' object is not iterable 源码 def get_url(lines): urls=[] for line in lines: if 'i ...

  7. 移动端 cordova vue videojs 全屏播放后退出全屏返回后退出app问题

    问题描述 移动端上面使用了videojs 播放视频,同时也监听了手机返回事件document.addEventListener('backbutton',.接着我们点击全屏播放后在退出全屏在返回后直接 ...

  8. mysql frm、MYD、MYI数据文件恢复,导入MySQL中

    前言 .frm..MYI..MYD 文件分别是 MySQL 的 MyISAM存储引擎存储的表结构.索引.数据文件. 简单方法恢复数据 .frm..MYI..MYD文件如果直接以文本打开,全部会以二进制 ...

  9. String类的特点

    1.String类的特点 1.java程序中,只要是双引号字符串,就都是String类的对象 如图 只有String这一个类有这种特殊的创建对象方法 我们知道,只有对象才可以通过 点什么来调用方法,而 ...

  10. 如何使用Nacos作为配置中心统一管理配置

    如何使用Nacos作为配置中心统一管理配置 1).引入依赖, <dependency> <groupId>com.alibaba.cloud</groupId> & ...