Permutation

from itertools import permutations

arr = [1, 2, 3]
comb = permutations(arr, 2)
print(list(comb))
# output => [(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)]

comb2 = list(permutations(arr, 1))
print(comb2)
# output => [(1,), (2,), (3,)]

print('list(comb) :', list(comb), ', comb2 :', comb2)
# output => list(comb) : [] , comb2 : [(1,), (2,), (3,)]
# 왜 인지 모르지만 한번 사용한 건 다시 안된다. 처음부터 list로 받아야 재사용이 가능하다.

순열 : 순서가 있을 땐 combinations 사용


Combination

from itertools import combinations

arr = [1, 2, 3]
comb = combinations(arr, 2)
print(list(comb))
# output => [(1, 2), (1, 3), (2, 3)]

comb2 = list(combinations(arr, 1))
print(comb2)
# output => [(1,), (2,), (3,)]

print('list(comb) :', list(comb), ', comb2 :', comb2)
# output => list(comb) : [] , comb2 : [(1,), (2,), (3,)]
# 왜 인지 모르지만 한번 사용한 건 다시 안된다. 처음부터 list로 받아야 재사용이 가능하다.

조합 : 순서가 없을 땐 combinations 사용


중복순열, 중복조합에 관한 모듈도 있는데, 그건 아직 필요한적이 없었다.
product, combinations_with_replacement

이 두개인데, 나중에 사용하는 떄가 온다면 그 때 작성해도 될 것 같다.

0개의 댓글