product
product는 입력 반복 가능 객체의 데카르트 곱 (Cartesian product) 을 계산합니다. 임의의 수의 반복 가능 객체를 인수로 받으며, 각 입력 반복 가능 객체의 반복 횟수를 지정하는 선택적 repeat 매개변수를 사용합니다.
예시:
WebIDE 에서 product.py라는 프로젝트를 생성하고 다음 내용을 입력합니다.
import itertools
## 두 리스트의 데카르트 곱을 계산합니다.
list1 = [1, 2]
list2 = ['a', 'b']
product_iterator = itertools.product(list1, list2)
## product 반복자의 요소를 출력합니다.
for item in product_iterator:
print(item)
출력:
다음 명령을 사용하여 스크립트를 실행합니다.
python product.py
(1, 'a')
(1, 'b')
(2, 'a')
(2, 'b')
permutations
permutations는 입력 반복 가능 객체의 요소에서 가능한 모든 순서가 지정된 순열 (permutation) 을 생성합니다. 반복 가능 객체와 순열의 길이를 지정하는 선택적 정수 r을 인수로 받습니다.
예시:
WebIDE 에서 permutations.py라는 프로젝트를 생성하고 다음 내용을 입력합니다.
import itertools
## 리스트에서 길이 2 의 모든 순열을 생성합니다.
list1 = [1, 2, 3]
permutations_iterator = itertools.permutations(list1, 2)
## permutations 반복자의 요소를 출력합니다.
for item in permutations_iterator:
print(item)
출력:
다음 명령을 사용하여 스크립트를 실행합니다.
python permutations.py
(1, 2)
(1, 3)
(2, 1)
(2, 3)
(3, 1)
(3, 2)
combinations
combinations는 입력 반복 가능 객체의 요소에서 가능한 모든 순서가 지정되지 않은 조합 (combination) 을 생성합니다. 반복 가능 객체와 조합의 길이를 지정하는 정수 r을 인수로 받습니다.
예시:
WebIDE 에서 combinations.py라는 프로젝트를 생성하고 다음 내용을 입력합니다.
import itertools
## 리스트에서 길이 2 의 모든 조합을 생성합니다.
list1 = [1, 2, 3]
combinations_iterator = itertools.combinations(list1, 2)
## combinations 반복자의 요소를 출력합니다.
for item in combinations_iterator:
print(item)
출력:
다음 명령을 사용하여 스크립트를 실행합니다.
python combinations.py
(1, 2)
(1, 3)
(2, 3)
combinations_with_replacement
combinations_with_replacement는 입력 반복 가능 객체의 요소에서 가능한 모든 순서가 지정되지 않은 조합을 생성하며, 중복된 요소를 허용합니다. 반복 가능 객체와 조합의 길이를 지정하는 정수 r을 인수로 받습니다.
예시:
WebIDE 에서 cr.py라는 프로젝트를 생성하고 다음 내용을 입력합니다.
import itertools
## 리스트에서 길이 2 의 모든 중복 조합을 생성합니다.
list1 = [1, 2, 3]
combinations_iterator = itertools.combinations_with_replacement(list1, 2)
## combinations_with_replacement 반복자의 요소를 출력합니다.
for item in combinations_iterator:
print(item)
출력:
다음 명령을 사용하여 스크립트를 실행합니다.
python cr.py
(1, 1)
(1, 2)
(1, 3)
(2, 2)
(2, 3)
(3, 3)