Python 사용법들
파이썬 라이브러리를 이용한 순열과 조합
jhk828
2020. 9. 11. 21:53
순열 구하기
모든 경우의 수
import itertools as it
n = 3
a = list(range(1, n+1))
# 리스트 a로 만들 수 있는 모든 순열의 경우 tuple 형태로 출력
for tmp in it.permutations(a):
print(tmp)
# (1, 2, 3)
# (1, 3, 2)
# (2, 1, 3)
# (2, 3, 1)
# (3, 1, 2)
# (3, 2, 1)
for tmp in it.permutations([1, 2, 3], 2):
print(tmp)
# (1, 2)
# (1, 3)
# (2, 1)
# (2, 3)
# (3, 1)
# (3, 2)
조합 구하기
중복 제거
import itertools as it
for tmp in it.combinations([1, 2, 3], 2):
print(tmp)
# (1, 2)
# (1, 3)
# (2, 3)