순열

permutations(반복 가능한 객체, n) // n은 몇 개 뽑을지

  • 중복 허용하지 않음
  • 순서에 의미가 없음
from itertools import permutations

print(list(permutations([1,2,3,4], 2)))

# [(1, 2), (1, 3), (1, 4), (2, 1), (2, 3), (2, 4), (3, 1), (3, 2), (3, 4), (4, 1), (4, 2), (4, 3)]

 

중복 순열

product(반복 가능한 객체, n) // n은 몇 개 뽑을지

  • 중복 허용
from itertools import product

print(list(product([1,2,3,4], repeat=2)))

# [(1, 1), (1, 2), (1, 3), (1, 4), (2, 1), (2, 2), (2, 3), (2, 4), (3, 1), (3, 2), (3, 3), (3, 4), (4, 1), (4, 2), (4, 3), (4, 4)]

 

조합

combinations(반복 가능한 객체, n) // n은 몇개를 뽑을 것인지

  • 중복 허용하지 않음
  • 순서에 의미가 없음
from itertools import combinations

print(list(combinations([1,2,3,4], 2)))

# [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]

 

중복 조합

combinations_with_replacement(반복 가능한 객체, n) // n=몇개를 뽑을 것인지

  • 중복 허용
from itertools import combinations_with_replacement

print(list(combinations_with_replacement([1,2,3,4], 2)))

# [(1, 1), (1, 2), (1, 3), (1, 4), (2, 1), (2, 2), (2, 3), (2, 4), (3, 1), (3, 2), (3, 3), (3, 4), (4, 1), (4, 2), (4, 3), (4, 4)]
복사했습니다!