[python] itertools 라이브러리

이도원·2022년 9월 1일
0

python 라이브러리

목록 보기
4/6

효율적인 루핑을 위한 이터레이터를 만드는 함수

1. permutations() - 순열

permutations(iterable, r=None)
입력 iterable에서 요소의 연속된 길이 r 순열(모든 가능한 순서, 반복되는 요소 없음)을 반환
r이 지정되지 않거나 None이면, r의 기본값은 iterable의 길이

ex) permutations('ABCD', 2) -> AB AC AD BA BC BD CA CB CD DA DB DC
	permutations(range(3)) --> 012 021 102 120 201 210

2. combinations() - 조합

combinations(iterable, r)
입력 iterable에서 요소의 길이 r 서브 시퀀스들을 반환(정렬된 순서, 반복되는 요소 없음)
조합(combination) 튜플은 입력 iterable의 순서에 따라 사전식 순서로 방출

ex) combinations('ABCD', 2) -> AB AC AD BC BD CD
	combinations(range(4), 3) --> 012 013 023 123

3. product() - 곱셈

product(*iterables, repeat=1)
입력 이터러블들(iterables)의 데카르트 곱(제너레이터 표현식에서의 중첩된 for-루프와 동등)
이터러블의 자신과의 곱을 계산하려면, 선택적 repeat 키워드 인자를 사용하여 반복 횟수를 지정

ex) product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
	product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111

4. combinations_with_replacement()

combinations_with_replacement(iterable, r)
입력 iterable에서 요소의 길이 r 서브 시퀀스들을 반환(정렬된 순서, 반복되는 요소 있음)

ex) combinations_with_replacement('ABCD', 2) --> AA AB AC AD BB BC BD CC CD DD

공식문서

https://docs.python.org/ko/3/library/itertools.html#module-itertools

profile
studying

0개의 댓글