조합 (순서만 다르지 원소는 같다) - 1, 2, 3 - 2, 1, 3 같은 것이라 생각한다.
순열 - permutations : 중복 없는 순열 - product : 중복 있는 순열
from itertools import permutations # 중복 미포함
from itertools import product # 중복 포함
a = [1,2,3]
# 문법 틀
# permutations(반복 가능 객체, r)
# product(반복 가능 객체, repeat=2)
# print(list(permutations(a,2))) # 중복 없는 순열 => 요소 2개 뽑기
# print(list(product(a,'abc'))) # 중복 있는 순열 => a,b,c 문자를 포함한 순열
for i in product(a,repeat=3):
print(i)
print('-'*20)
for i in permutations(a,3):
print(i)
print('-'*20)
실행결과
product
permutations
조합 - combinations : 중복 없는 조합 - combinations_with_replacement : 중복 있는 조합
from itertools import combinations
from itertools import combinations_with_replacement as comr
a = [1,2,3]
# 문법 틀
# combinations(반복 가능 객체, r)
# combinations_with_replacement(반복 가능 객체, r
# print(list(combinations(a,2))) # 중복 없는 조합
# print(list(comr(a,2))) # 중복 있는 조합
# for i in comr(a,2):
# print(i)