标签:
Python的内建模块itertools
提供了非常有用的用于操作迭代对象的函数。
1、Infinite Iterators
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 |
|
1.1 count
创建一个迭代器,生成从n开始的连续整数,如果忽略n,则从0开始计算(注意:此迭代器不支持长整数)
如果超出了sys.maxint,计数器将溢出并继续从-sys.maxint-1开始计算。
>>> import itertools >>> n = itertools.count(1) >>> for i in n: print(i) 1 2 3 4 ......
1.2 cycle
传入一个序列,无限循环下去:
>>> itertools.cycle(‘ABCDE‘) <itertools.cycle object at 0x00000000033576C8> >>> for i in itertools.cycle(‘ABCDE‘): print(i) A B C D E A B .....
1.3 repeat
创建一个迭代器,重复生成object,times(如果已提供)指定重复计数,如果未提供times,将无止尽返回该对象。
>>> s = itertools.repeat(‘ABC‘,4) >>> s repeat(‘ABC‘, 4) >>> for i in s: print(i) ABC ABC ABC ABC >>>
2、Iterators terminating on the shortest input sequence
Iterator |
Arguments |
Results |
Example |
---|---|---|---|
|
p [,func] |
p0, p0+p1, p0+p1+p2, ... |
|
|
p, q, ... |
p0, p1, ... plast, q0, q1, ... |
|
|
iterable |
p0, p1, ... plast, q0, q1, ... |
|
|
data, selectors |
(d[0] if s[0]), (d[1] if s[1]), ... |
|
|
pred, seq |
seq[n], seq[n+1], starting when pred fails |
|
|
pred, seq |
elements of seq where pred(elem) is false |
|
|
iterable[, keyfunc] |
sub-iterators grouped by value of keyfunc(v) |
|
|
seq, [start,] stop [, step] |
elements from seq[start:stop:step] |
|
|
func, seq |
func(*seq[0]), func(*seq[1]), ... |
|
|
pred, seq |
seq[0], seq[1], until pred fails |
|
|
it, n |
it1, it2, ... itn splits one iterator into n |
|
|
p, q, ... |
(p[0], q[0]), (p[1], q[1]), ... |
|
2.1 chain
将多个迭代器作为参数, 但只返回单个迭代器, 它产生所有参数迭代器的内容, 就好像他们是来自于一个单一的序列.
>>> for c in itertools.chain(‘ABC‘, ‘XYZ‘): ... print(c) # 迭代效果:‘A‘ ‘B‘ ‘C‘ ‘X‘ ‘Y‘ ‘Z‘
2.2 groupby
返回一个产生按照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‘]
实际上挑选规则是通过函数完成的,只要作用于函数的两个元素返回的值相等,这两个元素就被认为是在一组的,而函数返回值作为组的key。如果我们要忽略大小写分组,就可以让元素‘A‘
和‘a‘
都返回相同的key:
>>> for key, group in itertools.groupby(‘AaaBBbcCAAa‘, lambda c: c.upper()): ... print(key, list(group)) ... A [‘A‘, ‘a‘, ‘a‘] B [‘B‘, ‘B‘, ‘b‘] C [‘c‘, ‘C‘] A [‘A‘, ‘A‘, ‘a‘]
3、Combinatoric generators
Iterator |
Arguments |
Results |
---|---|---|
|
p, q, ... [repeat=1] |
cartesian product, equivalent to a nested for-loop |
|
p[, r] |
r-length tuples, all possible orderings, no repeated elements |
|
p, r |
r-length tuples, in sorted order, no repeated elements |
|
p, r |
r-length tuples, in sorted order, with repeated elements |
|
|
|
|
|
|
|
|
|
|
|
|
3.1 product(*iterables[, repeat]) 笛卡尔积
创建一个迭代器,生成表示item1,item2等中的项目的笛卡尔积的元组,repeat是一个关键字参数,指定重复生成序列的次数。
>>> a = (1,2,3) >>> b = (‘A‘,‘B‘,‘C‘) >>> c = itertools.product(a,b) >>> for i in c: print(i) (1, ‘A‘) (1, ‘B‘) (1, ‘C‘) (2, ‘A‘) (2, ‘B‘) (2, ‘C‘) (3, ‘A‘) (3, ‘B‘) (3, ‘C‘)
3.2 permutations(iterable[, r]) 排列
创建一个迭代器,返回iterable中所有长度为r的项目序列,如果省略了r,那么序列的长度与iterable中的项目数量相同: 返回p中任意取r个元素做排列的元组的迭代器
>>> a = [1, 2, 3, 4] >>> s = [i for i in itertools.permutations(a,3)] # 从序列a中选出3个元素进行排列 >>> s [(1, 2, 3), (1, 2, 4), (1, 3, 2), (1, 3, 4), (1, 4, 2), (1, 4, 3), (2, 1, 3), (2, 1, 4), (2, 3, 1), (2, 3, 4), (2, 4, 1), (2, 4, 3), (3, 1, 2), (3, 1, 4), (3, 2, 1), (3, 2, 4), (3, 4, 1), (3, 4, 2), (4, 1, 2), (4, 1, 3), (4, 2, 1), (4, 2, 3), (4, 3, 1), (4, 3, 2)] >>> s_number = [i[0]*100 + i[1]*10 + i[2] for i in s] # 选出的3个数字组合成不重复的3位数 >>> s_number [123, 124, 132, 134, 142, 143, 213, 214, 231, 234, 241, 243, 312, 314, 321, 324, 341, 342, 412, 413, 421, 423, 431, 432] >>>
3.3 combinations(iterable, r) 组合
创建一个迭代器,返回iterable中所有长度为r的子序列,返回的子序列中的项按输入iterable中的顺序排序 (不带重复)
>>> a = [1, 2, 3, 4] >>> s = [i for i in itertools.combinations(a,2)] # 从序列a中选出2个不重复的元素 >>> s [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]
更多详细信息请查看官网介绍:https://docs.python.org/3.5/library/itertools.html
标签:
原文地址:http://www.cnblogs.com/suke99/p/5185495.html