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
另請原諒我的不求甚解。