Python itertools模塊

今天學了簡單好玩的模塊,itertools模塊,簡單的說,itertools模塊就是產生一個循環器

來看下這個模塊的功能都有哪些吧


無窮循環器

count()  從開始的數字一直數下去

     count(10)   #-->  10 11 12 13 14 15 …

     count(10,2) #-->  10 12 14 16 18 20 …

 

cycle()  不停的將可迭代元素進行循環

     cycle(‘abcd’)  #-->  a b c d a b c d a …

 

repeat()  不停的重複一個元素

     repeat(20)   #-->  20 20 20 20 20 …

     repeat(20,3) #-->  20 20 20

 

 

有限循環器

accumulate()  將可迭代的容器進行累加,也可以自己指定具體的操作

     accumulate(range(10))    #-->  0 1 3 6 10

     accumulate(range(1,5), func = operator.mul)    #-->  1 2 6 24

 

chain()  將幾個可迭代的容器依次迭代

     chain(range(5),'abcd')          #-->  0 1 2 3 4 a b c d

     chain(['abcd', 'bcde', 'cdef'])    #-->  abcd bcde cdef

 

chain.from_iterable()  將可迭代容器裏面的元素再次進行迭代一次

     chain.from_iterable(['abcd', 'bcde', 'cdef'])            #-->  a b c d b c d e c d e f

     chain.from_iterable([['abcd', 'bcde','cdef'],'abcd'])    #-->  abcd bcdecdef a b c d

 

compress()  兩個參數分別爲data, selectors, 根據selectors中的真假情況返回data中的元素

     compress('ABCDEF', [1,0,1,0,1,1])       #-->  A C E F

 

dropwhile()  原型爲dropwhile(predicate, iterable)predicate返回True時,跳過元素。一旦函數返回False,則開始收集剩下的所有元素到循環器

     dropwhile(lambda x: x<5, [1,4,6,4,1])    #-->  6 4 1

 

filterfalse()  原型爲filterfalse(predicate, iterable)predicate返回False時,纔將iterable中的元素添加進循環器

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

 

groupby()  原型爲groupby(iterable, key=None)key的結果作用於iterable中的元素,將擁有相同返回結果的元素加入到循環器中,該函數之前需要確保iterable是經過排序的

  示例一:

     for i,g in itertools.groupby('AAAABBBCCDAABBB'):

           print(i,list(g))

     結果:

      A ['A', 'A', 'A','A']

      B ['B', 'B', 'B']

      C ['C', 'C']

      D ['D']

      A ['A', 'A']

      B ['B', 'B', 'B']

  示例二:

     def height_classify(h):

           if h>180 :

                 return 'tall'

           elif h< 160 :

                 return 'short'

           else :

                 return 'middle'

     friends = [192, 158, 168, 195, 185, 170, 135,174, 182]

     friends = sorted(friends, key =height_classify)

     for m, n in itertools.groupby(friends, key =height_classify):

           print(m)

           print(list(n))

     結果:

      middle

      [168, 170, 174]

      short

      [158, 135]

      tall

      [192, 195, 185, 182]

 

islice() 原型爲islice(iterable, stop)islice(iterable, start, stop[, step])

     islice('ABCDEFG', 2)             #-->  A B

     islice('ABCDEFG', 2, 4)          #-->  C D

     islice('ABCDEFG', 2, None)      #-->  C D E F G

     islice('ABCDEFG', 0, None, 2)   #-->  A C E G

 

starmap() 原型爲starmap(function, iterable)

     starmap(pow, [(2,5), (3,2), (10,3)])      #-->  32 9 1000

 

takewhile() 原型爲takewhile(predicate,iterable),與dropwhile()功能相反

     takewhile(lambda x: x<5, [1,4,6,4,1])    #--> 1 4

 

tee() 原型爲tee(iterable, n=2); 從單個的iterable返回n個獨立的循環器

 

zip_longest() 原型爲zip_longest(*iterables,fillvalue=None)

     zip_longest('abcd','123',fillvalue='*')      #-->  ('a', '1')('b','2')('c', '3')('d', '*')

 

 

組合生成器

product() 原型爲product(*iterables, repeat=1)做笛卡爾乘積

     product('abc','xy')    #-->  ('a', 'x')('a', 'y')('b', 'x')('b', 'y')('c','x')('c', 'y')

 

permutations() 原型爲permutations(iterable,r=None)全排列

     permutations('abc')    #-->  ('a', 'b', 'c')('a', 'c', 'b')('b', 'a', 'c')('b','c', 'a')('c', 'a', 'b')('c', 'b', 'a')

     permutations('abcd', r=2)    #-->  ('a', 'b')('a', 'c')('a', 'd')('b', 'a')('b','c')('b', 'd')('c', 'a')('c', 'b')('c', 'd')('d', 'a')('d', 'b')('d', 'c')

 

combinations() 原型爲combinations(iterable, r)

     combinations('abcd',r=3)    #-->  ('a', 'b', 'c')('a', 'b', 'd')('a', 'c', 'd')('b','c', 'd')

 

combinations_with_replacement() 原型爲combinations_with_replacement(iterable,r)

     combinations_with_replacement('abc',r=3)    #-->  ('a', 'a', 'a')('a', 'a', 'b')('a', 'a', 'c')('a','b', 'b')('a', 'b', 'c')('a', 'c', 'c')('b', 'b', 'b')('b', 'b', 'c')('b', 'c','c')('c', 'c', 'c')

 



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章