파이썬에서 순열과 조합은 itertools 모듈을 사용해서 구할 수 있다.
-
순열이란 서로 다른 n개의 원소에서 r개를 중복없이 순서에 상관있게 선택하는 혹은 나열하는 것이다.
-
permutations 사용법
from itertools import permutations
nums = [1, 2, 3]
permutation = permutations(nums, 2)
print(list(permutation))
# 결과 출력
[(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)]
-
서로 다른 n개의 원소를 갖는 어떤 집합에서 r개의 원소를 선택하는 것이이다. 선택의 순서와 상관없이 같은 원소들이 선택되었다면 같은 조합이다.
-
combinations 사용법
from itertools import combinations
nums = [1, 2, 3]
combination = combinations(nums, 2)
print(list(combination))
# 결과 출력
[(1, 2), (1, 3), (2, 3)]