KKG
Programming
KKG
전체 방문자
오늘
어제
  • 전체 글 보기 (84)
    • 회고 (9)
    • Bootcamp (19)
    • Error Handling (2)
    • Kotlin (1)
    • Java (19)
      • Java (14)
      • Spring (1)
      • JPA (2)
      • Link (2)
    • Python (5)
    • 알고리즘 (20)
      • 알고리즘 (4)
      • 백준 (14)
      • 프로그래머스 (1)
      • Link (1)
    • SQL (5)
      • SQL (1)
      • MySQL (4)
    • Web (2)
    • etc (1)

블로그 메뉴

  • 태그
  • 방명록
  • 깃허브

인기 글

티스토리

hELLO · Designed By 정상우.
KKG

Programming

Python

[Python] 중복순열, 중복조합(itertools.product, combinations_with_replacement)

2022. 1. 31. 12:18
import itertools

alphabet = ['A', 'B', 'C'] # 문자열이나 range로도 가능
print([*itertools.product(alphabet, alphabet)]) # 2개의 원소로 중복순열
print([*itertools.product(alphabet, repeat=3)]) # 3개의 원소로 중복순열

print()

print([*itertools.combinations_with_replacement(alphabet, 2)]) # 2개의 원소로 중복조합
출력값
[('A', 'A'), ('A', 'B'), ('A', 'C'), ('B', 'A'), ('B', 'B'), ('B', 'C'), ('C', 'A'), ('C', 'B'), ('C', 'C')]
[('A', 'A', 'A'), ('A', 'A', 'B'), ('A', 'A', 'C'), ('A', 'B', 'A'), ('A', 'B', 'B'), ('A', 'B', 'C'), ('A', 'C', 'A'), ('A', 'C', 'B'), ('A', 'C', 'C'), ('B', 'A', 'A'), ('B', 'A', 'B'), ('B', 'A', 'C'), ('B', 'B', 'A'), ('B', 'B', 'B'), ('B', 'B', 'C'), ('B', 'C', 'A'), ('B', 'C', 'B'), ('B', 'C', 'C'), ('C', 'A', 'A'), ('C', 'A', 'B'), ('C', 'A', 'C'), ('C', 'B', 'A'), ('C', 'B', 'B'), ('C', 'B', 'C'), ('C', 'C', 'A'), ('C', 'C', 'B'), ('C', 'C', 'C')]

[('A', 'A'), ('A', 'B'), ('A', 'C'), ('B', 'B'), ('B', 'C'), ('C', 'C')]

itertools 없이 직접구현

    'Python' 카테고리의 다른 글
    • [Python] collections.Counter
    • [Python] 순열, 조합(itertools.permutations, combinations)
    • [Python] 누적합 itertools.accumulate
    • [Python] 힙 자료구조 Heapqueue

    티스토리툴바