순열, 조합, 중복 순열, 중복 조합

용가리🐉·2024년 6월 9일
0

기타

목록 보기
4/5
post-thumbnail

4가지 모두 주어진 요소들 중에서 선택하는 것을 나타내는 방법!
항상 헷갈리기에 한 번에 각 차이를 정리하고자 한다.

순서 상관이란?

  • 배열의 요소가 같을 때,
    • 요소의 나열 순서가 중요! → 순서 상관 O
      • 나열 순서가 다르면 다른 배열
    • 요소의 나열 순서가 중요하지 않음! → 순서 상관 X
      • 나열 순서가 달라도 같은 배열

[1, 2, 3]의 배열에서 2가지 요소를 뽑을 경우

순열

  • 순서 상관 O
  • 중복 허용 X

경우: 12, 13, 21, 23, 31, 32 (총 6개)

from itertools import permutations

data = [1, 2, 3]
permutations = list(permutations(data, 2))
print("순열:", permutations)

조합

  • 순서 상관 X
  • 중복 허용 X

경우: 12, 13, 23 (총 3개)

from itertools import combinations

data = [1, 2, 3]
combinations = list(combinations(data, 2))
print("조합:", combinations)

중복 순열

  • 순서 상관 O
  • 중복 허용 O

경우: 11, 12, 13, 21, 22, 23, 31, 32, 33 (총 9개)

from itertools import product

data = [1, 2, 3]
product = list(product(data, repeat=2))
print("중복 순열:", product)

중복 조합

  • 순서 상관 X
  • 중복 허용 O

경우: 11, 12, 13, 22, 23, 33 (총 6개)

from itertools import combinations_with_replacement

data = [1, 2, 3]
combinations_with_replacement = list(itertools.combinations_with_replacement(data, 2))
print("중복 조합:", combinations_with_replacement)
profile
자아를 찾아 떠나는 중,,,

0개의 댓글

관련 채용 정보