# Python 排列组合的实现

• np.random.permutation()
• itertools.combinations()
• itertools.permutations()

# 1. 对0-5之间的数进行一次全排列
>>>np.random.permutation(6)
array([3, 1, 5, 4, 0, 2])
# 2. 创建待排矩阵
>>>A = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
# 3. shuffle矩阵A
>>>p = np.random.permutation(A.shape[0])
>>>p
array([1, 2, 0])
>>>A[p, :]
array([[ 5,  6,  7,  8],
[ 9, 10, 11, 12],
[ 1,  2,  3,  4]])

### 的实现

>>>from itertools import combinations
>>>combins = [c for c in  combinations(range(5), 2)]
>>>len(combins)
10
>>>combins               # 而且是按序排列
[(0, 1), (0, 2), (0, 3), (0, 4), (1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]

>>>c = [c for c in combinations(range(A.shape[0]), 2)]
>>>A[c[0], :]           # 一种排列
array([[1, 2, 3, 4],
[5, 6, 7, 8]])

>>>from itertools import permutations
>>>pertumations(range(5), 2)
<itertools.permutations object at 0x0233E360>
>>>perms = permutations(range(5), 2)
>>>perms
[(0, 1), (0, 2), (0, 3), (0, 4), (1, 0), (1, 2), (1, 3), (1, 4), (2, 0), (2, 1),
(2, 3), (2, 4), (3, 0), (3, 1), (3, 2), (3, 4), (4, 0), (4, 1), (4, 2), (4, 3)]
>>>len(perms)
20