box = [a, b, c]
permutation(순열)
combination(조합)
"이터레이터"는 바로 볼 수 없다?"
- itertool은 "이터레이터"를 반환하기 때문에 모든 값을 메모리에 저장하지 않고 필요할 때 값을 저장한다.
- 따라서 이터레이터의 값을 확인하기 위해서는 밑에 두 방법을 사용해야 한다.
- 리스트 변환 후 출력
- 반복문을 사용해서 출력
itertools.combinations
문법: combinations(iterable, r)
특징:
from itertools import combinations
a = ['A', 'B', 'C']
result = combinations(a, 2)
print(list(result)) # 출력: [('A', 'B'), ('A', 'C'), ('B', 'C')]
itertools.permutation
문법: permutations(iterable, r=None)
특징:
from itertools import permutations
a = ['A', 'B', 'C']
result = permutations(a, 2)
print(list(result)) # 출력: [('A', 'B'), ('A', 'C'), ('B', 'A'), ('B', 'C'), ('C', 'A'), ('C', 'B')]