python迭代器Itertools

https://docs.python.org/3.6/library/itertools.html
一无限迭代器:
| Iterator | Arguments | Results | Example |
|---|---|---|---|
|
|
start, [step] |
start, start+step, start+2*step, ... |
|
|
|
p |
p0, p1, ... plast, p0, p1, ... |
|
|
|
elem [,n] |
elem, elem, elem, ... endlessly or up to n times |
|
- itertools.count(arg1,arg2):arg1:开始位置,arg2:步长
#相当于for i in range(,) #创建迭代器,从10开始步长为2,无结束 >>> import itertools
>>> n = itertools.count(,)
>>> print(type(n))
<class 'itertools.count'>
>>> for i in n:
print(i)
itertools.count()
- itertools.cycle(itertable)
生成一个无限循环可迭代参数的迭代器
>>> itertools.cycle('ABCDE')
<itertools.cycle object at 0x00000000033576C8>
>>> for i in itertools.cycle('ABCDE'):
print(i) #ABCDEABCDE.......
itertools.cycle()
- itertools.repeat(arg1,arg2):arg1:要重复的参数,arg2:重复多少遍
>>> s = itertools.repeat('ABC',)
>>> s
repeat('ABC', )
>>> for i in s:
print(i)
#ABC
#ABC
#ABC
itertools.repeat()
二处理输入序列迭代器
| 迭代器 | 参数 | 结果 | 例 |
|---|---|---|---|
accumulate() |
p [,func] | p0,p0 + p1,p0 + p1 + p2,... | accumulate([1,2,3,4,5]) --> 1 3 610 15 |
chain() |
p,q,... | p0,p1,... plast,q0,q1,... | chain('ABC', 'DEF') --> A B C D EF |
chain.from_iterable() |
迭代 | p0,p1,... plast,q0,q1,... | chain.from_iterable(['ABC','DEF']) --> A B C D E F |
compress() |
数据,选择器 | (d [0]如果s [0]),(d [1]如果s [1]),...... | compress('ABCDEF', [1,0,1,0,1,1])--> A C E |
| pred,seq | seq [n],seq [n + 1],当pred失败时开始 | dropwhile(lambda x: x<5,[1,4,6,4,1]) --> 6 4 1 |
|
filterfalse() |
pred,seq | seq的元素,其中pred(elem)是假的 | filterfalse(lambda x: x%2,range(10)) --> 0 2 4 6 8 |
groupby() |
可迭代的[,关键] | 按键值(v)分组的子迭代器 | |
islice() |
seq,[start,] stop [,step] | 来自seq [start:stop:step]的元素 | islice('ABCDEFG', 2, None) --> CD 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],直到pred失败 | takewhile(lambda x: x<5,[1,4,6,4,1]) --> 1 4 |
tee() |
它,n | it1,it2,... itn将一个迭代器拆分为n | |
zip_longest() |
p,q,... | (p [0],q [0]),(p [1],q [1]),... | zip_longest('ABCD', 'xy',fillvalue='-') --> Ax By C- D- |
- itertools.accumulate(terable ,func )
创建一个迭代器,返回累积的总和,或其他二进制函数的累计结果(通过可选的func参数指定 )。如果提供了func,它应该是两个参数的函数。输入可迭代的 元素可以是可以被接受为func的参数的任何类型,如果输入iterable为空,则输出iterable也将为空。
>>> import operator
>>> n=itertools.accumulate([,,,])
>>> for i in n:
... print(i)# >>> n=itertools.accumulate([,,,],func=operator.mul)
>>> for i in n:
... print(i) #
itertools.accumulate()
- itertools.
chain(*iterable)
将俩个可迭代对象缝合起来,生成一个迭代器
>>> n=itertools.chain('abc','def')
>>> for i in n:
... print(i)
...
a
b
c
d
e
f
itertools.chain()
- classmethod
chain.from_iterable(iterable )对于一个可迭代的数据生成一个迭代器
>>> for i in itertools.chain.from_iterable(["abc","def"]):
... print(i)
...
a
b
c
d
e
f
- itertools.compress(data, selectors) :选择那些字符作为迭代器内容
#选择那些字符输出
>>> for i in itertools.compress("ABCDEFG",[,,,,,,]):
... print(i)
...
A
C
D
E
itertools.compress()
- itertools.dropwhile(pred,seq):当不满足pred条件时,截取后面的数据做为迭代器内容
dropwhile(lambda x: x<, [,,,,]) -->
- itertools.groupby(iterable,key)
返回一个产生按照key进行分组后的值集合的迭代器.
如果iterable在多次连续迭代中生成了同一项,则会定义一个组,如果将此函数应用一个分类列表,那么分组将定义该列表中的所有唯一项,key(如果已提供)是一个函数,应用于每一项,如果此函数存在返回值,该值将用于后续项而不是该项本身进行比较,此函数返回的迭代器生成元素(key, group),其中key是分组的键值,group是迭代器,生成组成该组的所有项。
>>> for key, group in itertools.groupby('AAABBBCCAAA'):
... print(key, list(group))
...
A ['A', 'A', 'A']
B ['B', 'B', 'B']
C ['C', 'C']
A ['A', 'A', 'A']
itertools.group()
- itertools.starmap(func,seq)
starmap(pow, [(,), (,), (,)]) -->
- itertools.tee(iterable,n)
>>> for i in itertools.tee([,,],):
... for x in i:
... print(x)
...
三:组合迭代器
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(*iterables[, repeat]) 笛卡尔积
创建一个迭代器,生成表示item1,item2等中的项目的笛卡尔积的元组,repeat是一个关键字参数,指定重复生成序列的次数
b=('a','b'.'c'),a=(,,),c=('d','e','f')
>>> c=itertools.product(a,b,c)
>>> for i in c:
... print(i)
(, 'a', 'd')
(, 'a', 'e')
(, 'a', 'f')
(, 'b', 'd')
(, 'b', 'e')
(, 'b', 'f')
(, 'c', 'd')
(, 'c', 'e')
(, 'c', 'f')
(, 'a', 'd')
(, 'a', 'e')
(, 'a', 'f')
(, 'b', 'd')
(, 'b', 'e')
(, 'b', 'f')
(, 'c', 'd')
(, 'c', 'e')
(, 'c', 'f')
(, 'a', 'd')
(, 'a', 'e')
(, 'a', 'f')
(, 'b', 'd')
(, 'b', 'e')
(, 'b', 'f')
(, 'c', 'd')
(, 'c', 'e')
(, 'c', 'f')
- itertools.permutation(iterable[, r]),按指定每个元素长度,返回所有有重复的排列,但不允许一个元素中有相同要的子元素
>>> a = [, , , ]
>>> s = [i for i in itertools.permutations(a,)] # 从序列a中选出3个元素进行组合排列
>>> s
[(, , ), (, , ), (, , ), (, , ), (, , ), (, , ), (, , ), (, , ), (, , ), (, , ), (, , ), (, , ), (, , ), (, , ), (, , ), (, , ), (, , ), (, , ), (, , ), (, , ), (, , ), (, , ), (, , ), (, , )]
>>> from itertools import permutations
>>> print permutations(['','',''])
<itertools.permutations object at 0x02A45210>
>>>
>>> print list(permutations(['','','']))
[('', '', ''), ('', '', ''), ('', '', ''), ('', '', ''), ('', '', ''), ('', '', '')]
>>>
>>> print list(permutations(['','',''],))
[('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', '')]
>>>
>>> print list(permutations('abc',))
[('a', 'b', 'c'), ('a', 'c', 'b'), ('b', 'a', 'c'), ('b', 'c', 'a'), ('c', 'a', 'b'), ('c', 'b', 'a')]
- itertools.combinations(iterable, r) 按指定长度,返回所有无重复组合,且每个元素中的子元素不能重复(如AA不可以)
>>> a = [, , , ]
>>> s = [i for i in itertools.combinations(a,)] # 从序列a中选出2个不重复的元素
>>> s
[(, ), (, ), (, ), (, ), (, ), (, )]
- itertools.combinations(iterable,r):和上面的一样,只是允许每个元素中的子元素可以相同(AA可以)
>>> from itertools import combinations_with_replacement
>>>
>>> print list(combinations_with_replacement('',))
[('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', ''), ('', '')]
>>>
>>> A = [,,,,]
>>> print list(combinations(A,))
[(, ), (, ), (, ), (, ), (, ), (, ), (, ), (, ), (, ), (, )]
python迭代器Itertools的更多相关文章
- python基础===Python 迭代器模块 itertools 简介
本文转自:http://python.jobbole.com/85321/ Python提供了一个非常棒的模块用于创建自定义的迭代器,这个模块就是 itertools.itertools 提供的工具相 ...
- python迭代器与iter()函数实例教程
python迭代器与iter()函数实例教程 发布时间:2014-07-16编辑:脚本学堂 本文介绍了python迭代器与iter()函数的用法,Python 的迭代无缝地支持序列对象,而且它还允许程 ...
- python 之 itertools模块
官方:https://yiyibooks.cn/xx/python_352/library/itertools.html 参考: https://blog.csdn.net/neweastsun/ar ...
- Python 迭代器和生成器(转)
Python 迭代器和生成器 在Python中,很多对象都是可以通过for语句来直接遍历的,例如list.string.dict等等,这些对象都可以被称为可迭代对象.至于说哪些对象是可以被迭代访问的, ...
- python基础=== itertools介绍(转载)
原文链接:http://python.jobbole.com/85321/ Python提供了一个非常棒的模块用于创建自定义的迭代器,这个模块就是 itertools.itertools 提供的工具相 ...
- Python中itertools.groupby分组的使用
Python中itertools.groupby分组的使用 有时候我们需要给一个列表按照某个属性分组,可以借助groupby来实现. 比如:一下列表我想以严重程度给它分组,并求出每组的元素个数. fr ...
- 【Python开发】Python:itertools模块
Python:itertools模块 itertools模块包含创建有效迭代器的函数,可以用各种方式对数据进行循环操作,此模块中的所有函数返回的迭代器都可以与for循环语句以及其他包含迭代器(如生成器 ...
- Python迭代器,可迭代对象,生成器
迭代器 迭代器(iterator)有时又称游标(cursor)是程式设计的软件设计模式,可在容器物件(container,例如链表或阵列)上遍访的界面,设计人员无需关心容器物件的内存分配的实现细节. ...
- Python 迭代器和列表解析
Python 迭代器和列表解析 1)迭代器 一种特殊的数据结构,以对象形式存在 >>> i1 = l1.__iter__() >>> i1 = iter(l1) 可 ...
随机推荐
- 如何解决Redis中的key过期问题
最近我们在Redis集群中发现了一个有趣的问题.在花费大量时间进行调试和测试后,通过更改key过期,我们可以将某些集群中的Redis内存使用量减少25%. Twitter内部运行着多个缓存服务.其中一 ...
- maven私服nexus清理释放磁盘空间
应用背景: 自建的maven私服(或者叫私仓)nexus在使用过程中,因很多服务不断迭代更新上传jar包至nexus中,底层存放在一个叫Blob Stores的存储中,最近发现该存储已增大至好几百G, ...
- LeetCode--11_Container_With_Most_Water
题目链接:点击这里 首先我们不考虑高度的话 最大的面积应该是l r 应该是最边上的值 ,我们要取最大 所以 要维护从左到右单调增,从右到左 单调增 这样我们才能保证 面积增加 public stati ...
- BZOJ2287 消失之物
这题貌似是个权限题qwq,我是用离线题库+本地数据包测的 题目大意: 给你\(n\)个体积分别为\(w[i]\)的物品和容积\(m\),问你将每一件物品分别去掉之后,拼出\(1\)~\(m\)中每一个 ...
- java中getAttribute与getParameter方法的区别
知识点1:getAttribute表示从request范围取得设置的属性,必须要先setAttribute设置属性,才能通过getAttribute来取得,设置与取得的为object对象类型 例: r ...
- 背包九讲PDF
本资料仅限个人学习交流使用,不得用于商业用途. 背包九讲PDF:https://pan.baidu.com/s/17rTxMwCo9iSTOW77yucdXQ 提取码:xbqa
- uCos-II移值(一)
os_cpu.h文件 该文件主要是完成操作系统使用的内部数据类型.常数以及宏的定义,这些都是与处理器平台密切相关的: 第一部分 以下部分定义了系统内部常用的数据类型,为了增加系统的可移植性,系统内核只 ...
- 将Python3导出为exe程序
一.pyinstaller简介 Python是一个脚本语言,被解释器解释执行.它的发布方式: .py文件:对于开源项目或者源码没那么重要的,直接提供源码,需要使用者自行安装Python并且安装依赖的各 ...
- vue 点击展开显示更多 点击收起部分隐藏
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- JDK8源码阅读之Collection及相关方法
最近面试总会被问到JDK8中的一些新特性,所以闲下来抽时间看了一下8的源码,目前主要看的是数据结构部分,特此记录一下. 新增函数式接口,实现该接口的可以直接用lambda表达式. default和st ...