티스토리 뷰
순열 구하기
모든 경우의 수
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)
'Python 사용법들' 카테고리의 다른 글
파이썬 sort()에서 람다/lambda 함수로 key 값 주기 (0) | 2020.09.12 |
---|---|
파이썬에서 반올림, 내장함수 round() (0) | 2020.09.12 |
파이썬 sort와 sorted 차이점 (0) | 2020.09.12 |
파이썬의 깊은 복사와 얕은 복사 (0) | 2020.09.12 |
댓글