itertools库
迭代器(生成器)在Python中是一种很常用也很好用的数据结构,比起列表(list)来说,迭代器最大的优势就是延迟计算,按需使用,从而提高开发体验和运行效率,以至于在Python 3中map,filter等操作返回的不再是列表而是迭代器。

话虽这么说但大家平时用到的迭代器大概只有range了,而通过iter函数把列表对象转化为迭代器对象又有点多此一举,这时候我们今天的主角itertools就该上场了。

使用itertools
itertools中的函数大多是返回各种迭代器对象,其中很多函数的作用我们平时要写很多代码才能达到,而在运行效率上反而更低,毕竟人家是系统库。

itertools.accumulate
简单来说就是累加。

>>> import itertools
>>> x = itertools.accumulate(range(10))
>>> print(list(x))

[0, 1, 3, 6, 10, 15, 21, 28, 36, 45]

itertools.chain
连接多个列表或者迭代器。

>>> x = itertools.chain(range(3), range(4), [3,2,1])
>>> print(list(x))
[0, 1, 2, 0, 1, 2, 3, 3, 2, 1]

itertools.combinations
求列表或生成器中指定数目的元素不重复的所有组合

>>> x = itertools.combinations(range(4), 3)
>>> print(list(x))
[(0, 1, 2), (0, 1, 3), (0, 2, 3), (1, 2, 3)]

itertools.combinations_with_replacement
允许重复元素的组合

>>> x = itertools.combinations_with_replacement('ABC', 2)
>>> print(list(x))
[('A', 'A'), ('A', 'B'), ('A', 'C'), ('B', 'B'), ('B', 'C'), ('C', 'C')]

itertools.compress
按照真值表筛选元素

>>> x = itertools.compress(range(5), (True, False, True, True, False))
>>> print(list(x))
[0, 2, 3]

itertools.count
就是一个计数器,可以指定起始位置和步长

>>> x = itertools.count(start=20, step=-1)
>>> print(list(itertools.islice(x, 0, 10, 1)))
[20, 19, 18, 17, 16, 15, 14, 13, 12, 11]

itertools.cycle
循环指定的列表和迭代器

>>> x = itertools.cycle('ABC')
>>> print(list(itertools.islice(x, 0, 10, 1)))
['A', 'B', 'C', 'A', 'B', 'C', 'A', 'B', 'C', 'A']

itertools.dropwhile
按照真值函数丢弃掉列表和迭代器前面的元素

>>> x = itertools.dropwhile(lambda e: e < 5, range(10))
>>> print(list(x))
[5, 6, 7, 8, 9]

itertools.filterfalse
保留对应真值为False的元素

>>> x = itertools.filterfalse(lambda e: e < 5, (1, 5, 3, 6, 9, 4))
>>> print(list(x))
[5, 6, 9]

itertools.groupby
按照分组函数的值对元素进行分组

>>> x = itertools.groupby(range(10), lambda x: x < 5 or x > 8)
>>> for condition, numbers in x:
...     print(condition, list(numbers))
True [0, 1, 2, 3, 4]
False [5, 6, 7, 8]
True [9]

itertools.islice
上文使用过的函数,对迭代器进行切片

>>> x = itertools.islice(range(10), 0, 9, 2)
>>> print(list(x))
[0, 2, 4, 6, 8]

itertools.permutations
产生指定数目的元素的所有排列(顺序有关)

>>> x = itertools.permutations(range(4), 3)
>>> print(list(x))
[(0, 1, 2), (0, 1, 3), (0, 2, 1), (0, 2, 3), (0, 3, 1), (0, 3, 2), (1, 0, 2), (1, 0, 3), (1, 2, 0), (1, 2, 3), (1, 3, 0), (1, 3, 2), (2, 0, 1), (2, 0, 3), (2, 1, 0), (2, 1, 3), (2, 3, 0), (2, 3, 1), (3, 0, 1), (3, 0, 2), (3, 1, 0), (3, 1, 2), (3, 2, 0), (3, 2, 1)]

itertools.product
产生多个列表和迭代器的(积)

>>> x = itertools.product('ABC', range(3))
>>>
>>> print(list(x))
[('A', 0), ('A', 1), ('A', 2), ('B', 0), ('B', 1), ('B', 2), ('C', 0), ('C', 1), ('C', 2)]

itertools.repeat
简单的生成一个拥有指定数目元素的迭代器

>>> x = itertools.repeat(0, 5)
>>> print(list(x))
[0, 0, 0, 0, 0]

itertools.starmap
类似map

**

>>> x = itertools.starmap(str.islower, 'aBCDefGhI')
>>> print(list(x))
[True, False, False, False, True, True, False, True, False]

**
itertools.takewhile
与dropwhile相反,保留元素直至真值函数值为假。

>>> x = itertools.takewhile(lambda e: e < 5, range(10))
>>> print(list(x))
[0, 1, 2, 3, 4]

itertools.tee
这个函数我也不是很懂,似乎是生成指定数目的迭代器

 >>> x = itertools.tee(range(10), 2)>>> for letters in x:...     print(list(letters))...
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

itertools.zip_longest
类似于zip,不过已较长的列表和迭代器的长度为准

>>> x = itertools.zip_longest(range(3), range(5))
>>> y = zip(range(3), range(5))
>>> print(list(x))
[(0, 0), (1, 1), (2, 2), (None, 3), (None, 4)]
>>> print(list(y))
[(0, 0), (1, 1), (2, 2)]

itertools库总结相关推荐

  1. itertools库

    前言 最近事情不是很多,想写一些技术文章分享给大家,同时也对自己一段时间来碎片化接受的知识进行一下梳理,所谓写清楚才能说清楚,说清楚才能想清楚,就是这个道理了. 很多人都致力于把Python代码写得更 ...

  2. Python itertools库详细教程

    前言 库的学习地址:https://pymotw.com/2/itertools/ 库的官网地址:https://docs.python.org/2/library/itertools.html 在P ...

  3. 这段代码很Pythonic | 相见恨晚的 itertools 库

    前言 最近事情不是很多,想写一些技术文章分享给大家,同时也对自己一段时间来碎片化接受的知识进行一下梳理,所谓写清楚才能说清楚,说清楚才能想清楚,就是这个道理了. 很多人都致力于把Python代码写得更 ...

  4. Python编程:itertools库排列组合

    itertools库包含: 无限迭代器 有限序列处理 排列组合 range对象 # 添加函数说明 def print_info(obj: "iter object") -> ...

  5. itertools库的使用方法

    itertools库 迭代器(生成器)在Python中是一种很常用也很好用的数据结构,比起列表(list)来说,迭代器最大的优势就是延迟计算,按需使用,从而提高开发体验和运行效率,以至于在Python ...

  6. Python笔记:itertools库简介

    Python笔记:itertools库简介 1. itertools库是什么 2. itertools库函数简介 1. 元素迭代相关 1. count 2. cycle 3. repeat 2. 排列 ...

  7. itertools库常用高效迭代器一览表,帮你快速实现数据的排列组合【python】

    itertools库常用高效迭代器一览表,帮你快速实现数据的排列组合 文档: https://docs.python.org/zh-cn/3/library/itertools.html iterto ...

  8. python itertools_Python之itertools库

    自己在做深度学习项目时,看到很多的大佬使用这个库,后来感觉真的使用这个库自己的代码量会减少很多,所以整理一下啦~~LeetCode这两天会接着开更,前几天忙着项目和考试了,哈哈,太烦了~~有很多需要整 ...

  9. python的itertools库_Python标准库itertools模块使用方法

    简介 官方描述:Functional tools for creating and using iterators.即用于创建高效迭代器的函数. itertools.chain(*iterable) ...

最新文章

  1. python全栈开发,Day40(进程间通信(队列和管道),进程间的数据共享Manager,进程池Pool)...
  2. MySql隔离级别多线程并发读取数据时的正确性
  3. 【aspnetcore】添加自定义json配置文件
  4. 如何在网页中插入Flv视频文件
  5. systemverilog 起步
  6. 1-4 数组元素的区间删除 (20 分)
  7. python2版本异常_Python to.exe引发异常:此项目的版本控制需要sdisttarb
  8. java 防止js注入_在WebView中如何让JS与Java安全地互相调用
  9. sqlite的编译、练习
  10. 浙大 PAT a1058
  11. Gse v0.40.0 发布,Go 高性能分词,增加更多常用 API
  12. java前后端分离是否会成为趋势
  13. visa虚拟卡生成器_虚拟银行卡汇总
  14. 《公路测设技术》课程网课最新作业测验考试
  15. matlab线性规划系列之基础解题
  16. python xlsm_“xlwings”:不支持写入.xlsm文件?
  17. 计算机提示无法验证发布者,win10 ie11提示由于无法验证发布者所以windows已经阻止此软件怎么办...
  18. Elasticsearch集群原理
  19. VSCode,插件安装失败,解决方法
  20. 流行和声(3)minor6和弦

热门文章

  1. 快客原创 火车头数据采集视频教程——第1讲 ecshop zencart shopex lightinthebox 网店数据批量采集教程
  2. 从0到1:搭建一个完整的kubernetes集群(kubeadm)
  3. c语言if文里null,C语言中if (p==NULL)的是与非
  4. linux登陆终端自动打开core文件功能
  5. 如何算出IP地址、子网掩码、网络地址、广播地址、可用IP地址。
  6. 100天精通Python丨基础知识篇 —— 08、Python 最常用的 20 个包(按使用频率排序)
  7. AJAX - XML简介、AJAX优缺点、HTTP协议
  8. 重温c语言 谭浩强 输出魔方阵 目前只做了基数魔方 以及 双偶幻方,单偶同理
  9. ng-dropdown-multiselect使用
  10. 阿里云oss文件上传工具类