1. 程式人生 > >python每日一類(5):itertools模塊

python每日一類(5):itertools模塊

rto ret bsp span max 創建 函數返回 log bre

itertools模塊包含創建有效叠代器的函數,可以用各種方式對數據進行循環操作,此模塊中的所有函數返回的叠代器都可以與for循環語句以及其他包含叠代器(如生成器和生成器表達式)的函數聯合使用。

chain(iter1, iter2, ..., iterN):

給出一組叠代器(iter1, iter2, ..., iterN),此函數創建一個新叠代器來將所有的叠代器鏈接起來,返回的叠代器從iter1開始生成項,知道iter1被用完,然後從iter2生成項,這一過程會持續到iterN中所有的項都被用完。

 1 from itertools import chain
 2 test = chain(‘AB‘, ‘CDE‘, ‘F‘)
 3 for el in test:
 4     print el
 5 
 6 A
 7 B
 8 C
 9 D
10 E
11 F

這裏再舉個斐波那契的例子:

from itertools import chain

class Fei:

    @staticmethod
    def fibon(n):
        a=b=1
        for i in range(n):
            yield a
            a,b=b,a+b


    def __iter__(self):
        return Fei.fibon(100)

fei=Fei()

test=chain(,fei)

for el in test:
    print(el)

  

chain.from_iterable(iterables):

一個備用鏈構造函數,其中的iterables是一個叠代變量,生成叠代序列,此操作的結果與以下生成器代碼片段生成的結果相同:

 1 >>> def f(iterables):
 2     for x in iterables:
 3         for y in x:
 4             yield y
 5 
 6 >>> test = f(‘ABCDEF‘)
 7 >>> test.next()
 8 ‘A‘
 9 
10 
11 >>> from itertools import chain
12 >>> test = chain.from_iterable(‘ABCDEF‘)
13 >>> test.next()
14 ‘A‘

  這裏說明一下:

combinations(iterable, r):

創建一個叠代器,返回iterable中所有長度為r的子序列,返回的子序列中的項按輸入iterable中的順序排序:

>>> from itertools import combinations
>>> test = combinations([1,2,3,4], 2)
>>> for el in test:
    print el

    
(1, 2)
(1, 3)
(1, 4)
(2, 3)
(2, 4)
(3, 4)

  

count([n]):

創建一個叠代器,生成從n開始的連續整數,如果忽略n,則從0開始計算(註意:此叠代器不支持長整數),如果超出了sys.maxint,計數器將溢出並繼續從-sys.maxint-1開始計算。

cycle(iterable):

創建一個叠代器,對iterable中的元素反復執行循環操作,內部會生成iterable中的元素的一個副本,此副本用於返回循環中的重復項。

dropwhile(predicate, iterable):

創建一個叠代器,只要函數predicate(item)為True,就丟棄iterable中的項,如果predicate返回False,就會生成iterable中的項和所有後續項。

 def dropwhile(predicate, iterable):
2     # dropwhile(lambda x: x<5, [1,4,6,4,1]) --> 6 4 1
3     iterable = iter(iterable)
4     for x in iterable:
5         if not predicate(x):
6             yield x
7             break
8     for x in iterable:
9         yield x

  

groupby(iterable [,key]):

創建一個叠代器,對iterable生成的連續項進行分組,在分組過程中會查找重復項。

如果iterable在多次連續叠代中生成了同一項,則會定義一個組,如果將此函數應用一個分類列表,那麽分組將定義該列表中的所有唯一項,key(如果已提供)是一個函數,應用於每一項,如果此函數存在返回值,該值將用於後續項而不是該項本身進行比較,此函數返回的叠代器生成元素(key, group),其中key是分組的鍵值,group是叠代器,生成組成該組的所有項。

ifilter(predicate, iterable):

創建一個叠代器,僅生成iterable中predicate(item)為True的項,如果predicate為None,將返回iterable中所有計算為True的項。

ifilter(lambda x: x%2, range(10)) --> 1 3 5 7 9

ifilterfalse(predicate, iterable):

創建一個叠代器,僅生成iterable中predicate(item)為False的項,如果predicate為None,則返回iterable中所有計算為False的項。

ifilterfalse(lambda x: x%2, range(10)) --> 0 2 4 6 8

imap(function, iter1, iter2, iter3, ..., iterN)

創建一個叠代器,生成項function(i1, i2, ..., iN),其中i1,i2...iN分別來自叠代器iter1,iter2 ... iterN,如果function為None,則返回(i1, i2, ..., iN)形式的元組,只要提供的一個叠代器不再生成值,叠代就會停止。

 >>> from itertools import *
 2   >>> d = imap(pow, (2,3,10), (5,2,3))
 3   >>> for i in d: print i
 4   
 5   32
 6   9
 7   1000
 8   
 9  ####
10  >>> d = imap(pow, (2,3,10), (5,2))
11  >>> for i in d: print i
12  
13  32
14  9
15 
16  ####
17  >>> d = imap(None, (2,3,10), (5,2))
18  >>> for i in d : print i
19  
20  (2, 5)
21  (3, 2)

  http://www.cnblogs.com/cython/articles/2169009.html

python每日一類(5):itertools模塊