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
Python 的詳細介紹:請點這裡
Python 的下載地址:請點這裡