首页 > 其他 > 详细

itertools库

时间:2017-09-08 00:58:27      阅读:283      评论:0      收藏:0      [点我收藏+]
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库

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!