码迷,mamicode.com
首页 > 其他好文 > 详细

itertools库

时间:2017-09-08 01:23:28      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:class   imap   doc   while   blog   out   product   目的   多个   

In [1]: import itertools

In [2]: dir(itertools)
Out[2]: 
[‘__doc__‘,
 ‘__file__‘,
 ‘__name__‘,
 ‘__package__‘,
 ‘chain‘,
 ‘combinations‘,
 ‘combinations_with_replacement‘,
 ‘compress‘,
 ‘count‘,
 ‘cycle‘,
 ‘dropwhile‘,
 ‘groupby‘,
 ‘ifilter‘,
 ‘ifilterfalse‘,
 ‘imap‘,
 ‘islice‘,
 ‘izip‘,
 ‘izip_longest‘,
 ‘permutations‘,
 ‘product‘,
 ‘repeat‘,
 ‘starmap‘,
 ‘takewhile‘,
 ‘tee‘]
  1. itertools.chain
    # 连接多个列表或生成器
    In [7]: list(itertools.chain(range(3), range(3,6), [6,7,8,9]))
    Out[7]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

     

  2. itertools.combinations
    # 求列表或生成器中指定数目的元素不重复的所有组合
    In [4]: list(itertools.combinations(range(5), 3))
    Out[4]: 
    [(0, 1, 2),
     (0, 1, 3),
     (0, 1, 4),
     (0, 2, 3),
     (0, 2, 4),
     (0, 3, 4),
     (1, 2, 3),
     (1, 2, 4),
     (1, 3, 4),
     (2, 3, 4)]

     

itertools库

标签:class   imap   doc   while   blog   out   product   目的   多个   

原文地址:http://www.cnblogs.com/jachin/p/7492566.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!