중복 순열, 중복 조합

bird.j·2021년 6월 9일
0

python

목록 보기
8/10

중복순열은 product를 쓰고 중복조합은 combinations_with_replacement인데, 둘의 공통점은 itertools모듈에서 임포트해서 사용한다는 것이고 다른점은

  • 중복순열은 product(반복 가능한 객체, repeat = 몇개씩 짝 지을 것인지)
from itertools import product

for i in product([1,2,3],repeat=2) :
    print(i,end=" ")

출력 : (1, 1) (1, 2) (1, 3) (2, 1) (2, 2) (2, 3) (3, 1) (3, 2) (3, 3)

  • 중복조합은 combinations_with_replacement(반복 가능한 객체, r(몇개씩))
from itertools import combinations_with_replacement

for i in combinations_with_replacement([1,2,3,4],2) :
    print(i,end=" ")

출력 : (1, 1) (1, 2) (1, 3) (1, 4) (2, 2) (2, 3) (2, 4) (3, 3) (3, 4) (4, 4)

파이썬 순열, 조합, 중복순열, 중복조합 쉽게 구하기

0개의 댓글