combinatoric iterators 종류
- product() - 중복순열
- permutations() - 순열
- combinations() - 조합
- combinations_with_replacement() - 중복조합
product()
- 중복 순열 : 중복을 허용하여 서로 다른 n개 중 r 개를 순서있게 나열하는 경우의 수(nπr)
*product는 다른 iterator과 다르게 repeat을 사용함.
from itertools import product
array = [1, 2, 3]
pro_array = list(product(array, repeat=2))
print(pro_array)
>>> [(1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3)]
permutations()
- 순열 : 서로 다른 n개에서 r개를 뽑아 일렬로 나열하는 방법의 수(nPr)
from itertools import permutations
array = [1, 2, 3]
permu_array = list(permutations(array, 2))
print(permu_array)
>>> [(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)]
combinations()
- 조합 : 서로 다른 n개 중에서 r개를 택하는 경우의 수(nCr)
from itertools import combinations
array = [1, 2, 3]
combi_array = list(combinations(array, 2))
print(combi_array)
>>> [(1, 2), (1, 3), (2, 3)]
combinations_with_replacement()
-중복조합 : 중복을 허용하여 n개 중 r개를 택하는 경우의 수(nHr)
from itertools import combinations_with_replacement
array = [1, 2, 3]
combi_array = list(combinations_with_replacement(array, 2))
print(combi_array)
>>> [(1, 1), (1, 2), (1, 3), (2, 2), (2, 3), (3, 3)]
'파이썬 스터디' 카테고리의 다른 글
최대공약수, 최소공배수 구하기 (0) | 2024.02.16 |
---|---|
소수 판별에서 시간 줄이기 (0) | 2024.01.05 |
join (1) | 2024.01.05 |
collections - Counter (0) | 2023.08.15 |
파이썬 표준 입출력(sep, end, rjust, ljust, zfill) (0) | 2023.07.29 |