Programmers (코딩 테스트 연습)
Python 순열/조합/중복순열/중복조합 구하기
xangmin
2021. 1. 27. 12:48
반응형
1. 순열
from itertools import permutations
for i in permutations([1,2,3,4], 2):
print(i, end=" ")
결과
(1, 2) (1, 3) (1, 4) (2, 1) (2, 3) (2, 4) (3, 1) (3, 2) (3, 4) (4, 1) (4, 2) (4, 3)
2. 조합
from itertools import combinations
for i in combinations([1,2,3,4], 2):
print(i, end=" ")
결과
(1, 2) (1, 3) (1, 4) (2, 3) (2, 4) (3, 4)
3. 중복순열
from itertools import product
for i in product([1,2,3],'ab'):
print(i, end=" ")
결과
(1, 'a') (1, 'b') (2, 'a') (2, 'b') (3, 'a') (3, 'b')
4. 중복조합
from itertools import combinations_with_replacement
for cwr in combinations_with_replacement([1,2,3,4], 2):
print(cwr, end=" ")
결과
(1, 1) (1, 2) (1, 3) (1, 4) (2, 2) (2, 3) (2, 4) (3, 3) (3, 4) (4, 4)
반응형