| 기능 | 함수 | 설명 |
|---|
| 순열 | permutations | 순서 O, 중복 X |
| 조합 | combinations | 순서 X, 중복 X |
| 중복 조합 | combinations_with_replacement | 순서 X, 중복 O |
| 데카르트 곱 | product | 중첩 루프를 간단히 처리 |
| 슬라이스 | islice | 이터레이터 슬라이싱 |
| 무한 카운터 | count | 무한 반복 (start부터 step씩 증가) |
| 필터링 | filterfalse | 조건이 거짓인 것만 추출 |
| 순환 | cycle | 반복해서 순환 |
| 고정반복 | repeat | 특정 값 반복 |
| 그룹핑 | groupby | 연속된 값 기준 그룹핑 |
1. 순열 / 조합 관련
permutations(iterable, r)
from itertools import permutations
list(permutations([1, 2, 3], 2))
combinations(iterable, r)
from itertools import combinations
list(combinations([1, 2, 3], 2))
combinations_with_replacement(iterable, r)
list(combinations_with_replacement([1, 2], 2))
2. 중첩 반복
product(*iterables, repeat=1)
from itertools import product
list(product([0, 1], repeat=2))
3. 무한 반복자
count(start=0, step=1)
from itertools import count
for i in count(10, 2):
if i > 20:
break
print(i)
cycle(iterable)
from itertools import cycle
c = cycle('AB')
[next(c) for _ in range(5)]
repeat(elem, n)
from itertools import repeat
list(repeat(3, 4))
4. 조건 / 필터링 관련
islice(iterable, start, stop, step)
from itertools import islice
list(islice(range(100), 10, 20, 2))
filterfalse(predicate, iterable)
from itertools import filterfalse
list(filterfalse(lambda x: x%2, range(5)))
5. 그룹핑/집계 관련
groupby(iterable, key=...)
from itertools import groupby
data = 'AAABBCCDAA'
grouped = [(k, list(g)) for k, g in groupby(data)]
요약
| 기능 | 기본 내장 코드 | itertools 코드 | 메모리 효율 | 속도 효율 |
|---|
조건 필터링 (True) | filter(func, iterable) | 동일 | ✅ | ✅ |
조건 필터링 (False) | x for x in ... if not | filterfalse(func, iterable) | ✅ | ✅ |
| 슬라이스 (이터레이터용) | 안됨 (list() 필요) | islice(iterable, start, stop, step) | ✅ | ✅ |
| 순열/조합/곱 | 직접 구현해야 함 | permutations, product 등 | ✅ | ✅ |