枚举列表的所有排列

时间:2012-06-04 11:52:31

标签: iphone objective-c math permutation combinations

  

可能重复:
  How to generate all permutations of a list in Python

我希望获得所有可能的组合,其值范围为0-7。

例如

arr[0]= 0,1,2,3,4,5,6,7
arr[1]=1,2,3,4,5,6,7,0
arr[2]=2,3,4,5,6,7,1,0

arr[3]= 0,1,2,3,4,5,6,7
arr[4]=1,2,3,4,5,6,7,0
arr[5]=2,3,4,5,6,7,1,0

arr[6]= 0,1,2,3,4,5,6,7
arr[7]=1,2,3,4,5,6,7,0
arr[8]=2,3,4,5,6,7,1,0

arr[9]= 0,1,2,3,4,5,6,7.....

等等。

我想得到所有可能的组合,没有来自给定值集的重复数字,即0-7。

1 个答案:

答案 0 :(得分:0)

确实这将取决于语言...这是python中的一个简短尝试:

>>> from itertools import permutations
>>> l = [0,1,2,3,4,5,6,7]
>>> for p in permutations(l):
    print p


(0, 1, 2, 3, 4, 5, 6, 7)
(0, 1, 2, 3, 4, 5, 7, 6)
(0, 1, 2, 3, 4, 6, 5, 7)
(0, 1, 2, 3, 4, 6, 7, 5)
(0, 1, 2, 3, 4, 7, 5, 6)
(0, 1, 2, 3, 4, 7, 6, 5)
...