python之itertools

 

  • 参考文档:
    • http://book.pythontips.com/en/latest/map_filter.html
    • https://docs.python.org/2/library/itertools.html
  • 无限迭代器count,两种用法
    • count()
    • count(start_pos, step)

    • cycle
      • cycle("ABC")
    • repeat
      • repeat(object, count)
  • 优先迭代器

chain

      • 接收多个可迭代对象作为参数,将它们『连接』起来,作为一个新的迭代器返回
      • 栗子:
        • chain(iterable), iterable为可迭代的对象
        • chain.from_iterable(iterable),返回一个迭代器

compress

      • 使用方式:compress(data, selectors)
      • compress 可用于对数据进行筛选,当 selectors 的某个元素为 true 时,则保留 data 对应位置的元素,否则去除

dropwhile

      • 使用方式:dropwhile(predicate, iterable)
      • 其中,predicate 是函数,iterable 是可迭代对象。对于 iterable 中的元素,如果 predicate(item) 为 true,则丢弃该元素,否则返回该项及所有后续项。

groupby

      • 使用方式:groupby(iterable[, keyfunc])
      • 其中,iterable 是一个可迭代对象,keyfunc 是分组函数,用于对 iterable 的连续项进行分组,如果不指定,则默认对 iterable 中的连续相同项进行分组,返回一个 (key, sub-iterator) 的迭代器
      • 解释:使用 len 函数作为分组函数,len对iterable-object的每个元素进行func,获取的结果作为key,具有相同len(ele)的ele作为一组value_iter;

ifilter

      • 使用方式:ifilter(function or None, sequence)
      • 将 iterable 中 function(item) 为 True 的元素组成一个迭代器返回,如果 function 是 None,则返回 iterable 中所有计算为 True 的项。
      • iterfalse
      • ifilterfalse 的使用形式和 ifilter 类似,它将 iterable 中 function(item) 为 False 的元素组成一个迭代器返回,如果 function 是 None,则返回 iterable 中所有计算为 False 的项。

islice

      • 使用方式:islice(iterable, [start,] stop [, step])
      • 其中,iterable 是可迭代对象,start 是开始索引,stop 是结束索引,step 是步长,start 和 step 可选。

imap

      • 使用方式:imap(func, iter1, iter2, iter3, ...)
      • imap 返回一个迭代器,元素为 func(i1, i2, i3, ...)i1i2 等分别来源于 iteriter2
      • list(imap(str, [1,2,3,4]))
      • list(imap(pow, [2, 3, 10], [4, 2, 3]))

map(not belogs to itertools)

      • 使用方式:map(func, list or iter)
      • map(lambda x:x**2, xrange(10))
      • map(lambda x:x**2, [2,3,4])

filter

      • 使用方式:filter(func, list or iter)
      • list(filter(lambda x:x<0, xrange(-5,5)))

reduce(not belongs to itertools)

      • reduce(func, list or iter)
      • reduce(lambda x,y: x*y, xrange(1, 4)) 
      • reduce(lambda x,y: x*y, [1,2,3,4])

tee

        • 使用形式:tee(iterable[, n])
        • tee 用于从 iterable 创建 n 个独立的迭代器,以元组的形式返回,n 的默认值是 2。
      takewhile
      • 使用方式:takewhile(predicate, iterable)
      • 其中,predicate 是函数,iterable 是可迭代对象。对于 iterable 中的元素,如果 predicate(item) 为 true,则保留该元素,只要 predicate(item) 为 false,则立即停止迭代。
    • zip(not belongs to itertools)
      • zip(list1 or iter1, list2 or iter2, ...)
      • such as:  zip(xrange(3,10), xrange(4,12), xrange(5,13))
    • izip
      • 使用方式:同zip
        izip(iter1, iter2, ..., iterN)
      • izip 用于将多个可迭代对象对应位置的元素作为一个元组,将所有元组『组成』一个迭代器,并返回。它的使用形式如下:
  • 组合生成器
    • 概述
      • product
      • permutations
      • combinations
      • combinations_with_replacement
    • product
      • 使用方式:product(iter1,iter2, ...., [repeat=1])
      • list(product('abcd', xrange(0,2)))
    • permutations
      • 使用方式:permutations(iterable[,r])
      • 栗子:list(permutations('ABC', 2))
    • combinations
      • 使用方式:combinations(iterable, r)
      • 用于求序列的组合
      •  

      • combinations_with_replacement
        • combinations_with_replacement 和 combinations 类似,但它生成的组合包含自身元素
        •  

posted @ 2018-05-20 11:27  征途2  阅读(60)  评论(0)    收藏  举报