python 2.6 引入了itertools模块,使得排列组合的实现非常简单:

import itertools  

有序排列:e.g., 4个数内选2个排列:

>>> print list(itertools.permutations([1,2,3,4],2))
[(1, 2), (1, 3), (1, 4), (2, 1), (2, 3), (2, 4), (3, 1), (3, 2), (3, 4), (4, 1), (4, 2), (4, 3)]

无序组合:e.g.,4个数内选2个:

>>> print list(itertools.combinations([1,2,3,4],2))
[(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]

 

原文转载自http://blog.csdn.net/flying881114/article/details/8482159

另请原谅我的不求甚解。

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-11
  • 2021-08-28
  • 2021-09-19
  • 2021-10-08
猜你喜欢
  • 2021-06-04
  • 2022-12-23
  • 2021-05-26
  • 2021-09-17
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案