서로 다른 n개의 원소에서 r개를 중복없이 순서에 상관있게 선택하는 것을 순열(permutation)이라고한다.
def perm(n,k):
if n == k:
print(arr)
else:
for j in range(n,k):
arr[n],arr[j] = arr[j],arr[n]
perm(n+1,k)
arr[n],arr[j] = arr[j],arr[n]
arr = [1,2,3]
N =len(arr)
perm(0,N) # (0,3)
#===============================
[1, 2, 3]
[1, 3, 2]
[2, 1, 3]
[2, 3, 1]
[3, 2, 1]
[3, 1, 2]
from itertools import permutations
arr = [1,2,3]
arr = list(permutations(arr,3))
print(arr)
#==================================
[(1, 2, 3), (1, 3, 2), (2, 1, 3),
(2, 3, 1), (3, 1, 2), (3, 2, 1)]
------------------------------------
for lst in arr:
print(lst)
#==================================
(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)