from itertools import permutations
items = ['1', '2', '3', '4', '5']
list(permutations(items, 2))
'''
[('1', '2'), ('1', '3'), ...]
'''
from itertools import combinations
items = ['1', '2', '3', '4', '5']
list(combinations(items, 2))
'''
[('1', '2'), ('1', '3'), ...]
'''
from itertools import product
items = [['a', 'b', 'c,'], ['1', '2', '3', '4'], ['!', '@', '#']]
list(product(*items))
'''
[('a', '1', '!'), ('a', '1', '@'), ...]
'''
https://docs.python.org/ko/3/library/itertools.html#itertools.product
입력 이터러블들(iterables)의 데카르트 곱.
아래 두 출력은 완전히 동일함
from itertools import product
A = [1, 2, 3]
B = [4, 5, 6]
for p in product(A, B):
print(p)
for p in [(a, b) for a in A for b in B]:
print(p)
(1, 's')
(1, 't')
(2, 's')
(2, 't')
자기 자신을 여러번 합쳐서 순회할 떄도 씀
from itertools import product
A = [1, 2]
for p in product(A, repeat=3):
print(p)
'''
(1, 1, 1)
(1, 1, 2)
(1, 2, 1)
(1, 2, 2)
(2, 1, 1)
(2, 1, 2)
(2, 2, 1)
(2, 2, 2)
'''