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)]

     

相关文章:

  • 2021-08-09
  • 2021-11-20
  • 2022-12-23
  • 2021-07-25
  • 2021-12-10
  • 2022-12-23
  • 2022-02-11
  • 2021-07-08
猜你喜欢
  • 2021-08-22
  • 2022-01-30
  • 2021-09-17
  • 2021-11-18
  • 2021-06-21
  • 2021-09-06
相关资源
相似解决方案