itertools模块主要功能是创建迭代器。

>>> itertools.
itertools.__class__(         itertools.chain(
itertools.__delattr__(       itertools.combinations(
itertools.__dict__           itertools.count(
itertools.__doc__            itertools.cycle(
itertools.__file__           itertools.dropwhile(
itertools.__format__(        itertools.groupby(
itertools.__getattribute__(  itertools.ifilter(
itertools.__hash__(          itertools.ifilterfalse(
itertools.__init__(          itertools.imap(
itertools.__name__           itertools.islice(
itertools.__new__(           itertools.izip(
itertools.__package__        itertools.izip_longest(
itertools.__reduce__(        itertools.permutations(
itertools.__reduce_ex__(     itertools.product(
itertools.__repr__(          itertools.repeat(
itertools.__setattr__(       itertools.starmap(
itertools.__sizeof__(        itertools.takewhile(
itertools.__str__(           itertools.tee(
itertools.__subclasshook__(

1.链接多个迭代器。

itertools.chain(iter1,iter2,...,itern):给出一组迭代器,此函数用来创建一个新的迭代器,来包括所给的迭代器的所有内容。

>>> name
['zhu', 'jiang', 'tao']
>>> a
[2, 4, 6]
>>> for x in itertools.chain(name,a):
...     print x,
...
zhu jiang tao 2 4 6

2.双层迭代

itertools.chain.from_iterable(iter) :对迭代器iter中的元素进行迭代,相当于代码:

for  it in iter:

   for x in it:

   yield x

>>> for x in itertools.chain.from_iterable(name):
...     print x,
...
z h u j i a n g t a o

3.创建一个迭代器,返回iterable中所有长度为r的子序列。

>>> for x in itertools.combinations(name,2):
...     print x
...
('zhu', 'jiang')
('zhu', 'tao')
('jiang', 'tao')
>>> for x in itertools.combinations(a,2):
...     print x,
...
(2, 4) (2, 6) (4, 6)

4.创建一个迭代器生成从n开始的连续整数

itertools.count(x)

>>> for x in itertools.count():
...     if x < 5:
...         print x
...     else:
...         break
...
0
1
2
3
4
>>> for x in itertools.count(3):
...     if x <= 10:
...         print x,
...     else:
...         break
...
3 4 5 6 7 8 9 10
#itertools.count(x) :生成连续的整数,x为开始值。默认为0


5.itertools.cycle(iterable):创建一个迭代器,对iterable元素,反复执行循环操作。

[root@zhu ~]# python zhu.py
['cai', 'sang', 'zi', 'cai', 'sang', 'zi', 'cai', 'sang', 'zi', 'cai', 'sang']
[root@zhu ~]# cat zhu.py
#!/usr/bin/python
import itertools
a = []
name  = 'cai sang zi'
name = name.split()
for x in itertools.cycle(name):
    if len(a) <= 10:
        a.append(x)
    else:
        print a
        break

6.dropwhile(predicate, iterable):创建一个迭代器,只要函数predicate(item)为真,就丢弃iterable中的项,如果predicate(item)为假,就会生成iterable中的项和后续的所有项。

[root@zhu ~]# python zhu.py
zi yue shang liu shao tou
[root@zhu ~]# cat zhu.py
#!/usr/bin/python
import itertools
name = 'cai sang zi yue shang liu shao tou'.split()
def zhu(s):
    if s == 'zi':
        return False
    else:
        return 2
for x in itertools.dropwhile(zhu,name):
    print x,
#一旦参数中的函数返回值为假时,才会迭代。

7.itertools.groupby(iterable):创建一迭代器,对iterable生成连续的项进行分组,如果iterable在连续迭代中生成了统一项,则会定义同一项。

>>> [list(g) for k, g in itertools.groupby('AAAABBBCCD')]
[['A', 'A', 'A', 'A'], ['B', 'B', 'B'], ['C', 'C'], ['D']]
>>> [k  for k, g in itertools.groupby('AAAABBBCCD')]
['A', 'B', 'C', 'D']
>>> [k  for k, g in itertools.groupby('AABAABDBBCCD')]
['A', 'B', 'A', 'B', 'D', 'B', 'C', 'D']
>>> [list(g) for k, g in itertools.groupby('AABAABDBBCCD')]
[['A', 'A'], ['B'], ['A', 'A'], ['B'], ['D'], ['B', 'B'], ['C', 'C'], ['D']]

8.imap(func, *iterables) --> imap object:创建一个迭代器,生成器func(i1,i2,i3,...,in)其中i1,i2,i3,...,in分别来自迭代器iter1,iter2,iter3,itern,如果func为none,则返回(i1,i2,i3,...,in)形式的元组,只要有一个迭代器不再生成值,迭代便停止。

[root@zhu ~]# cat zhu.py
#!/usr/bin/python
import itertools
a = [2,4,6,8]
b = [3,5,7,9]
def cheng(x,y):
    return x*y
for x in itertools.imap(cheng,a,b):
    print x
[root@zhu ~]# python zhu.py
6
20
42
72