😉 조합
from itertools import combinations # 조합(순서x)
nums=[1,2,3,4]
arr = list(combinations(nums, 3))
#출력: [(1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4)]
😉 순열
from itertools import permutations # 순열(순서 o)
nums=[1,2,3,4]
arr = list(permutations(nums, 3))
#출력: [(1, 2, 3), (1, 2, 4), (1, 3, 2), (1, 3, 4), (1, 4, 2), (1, 4, 3),
# (2, 1, 3), (2, 1, 4), (2, 3, 1), (2, 3, 4), (2, 4, 1), (2, 4, 3),
# (3, 1, 2), (3, 1, 4), (3, 2, 1), (3, 2, 4), (3, 4, 1), (3, 4, 2),
# (4, 1, 2), (4, 1, 3), (4, 2, 1), (4, 2, 3), (4, 3, 1), (4, 3, 2)]
😉 중복 조합
from itertools import combinations_with_replacement # 중복 조합
nums=[1,2,3,4]
M=2
for i in combinations_with_replacement(nums, M):
print(' '.join(map(str,i)))
#출력
1 1
1 2
1 3
1 4
2 2
2 3
2 4
3 3
3 4
4 4
😉 중복 순열
from itertools import product # 중복 순열
nums=[1,2,3,4]
M=2
for i in product(nums,repeat=M):
print(' '.join(map(str,i))) #int tuple -> string
#출력
1 1
1 2
1 3
1 4
2 1
2 2
2 3
2 4
3 1
3 2
3 3
3 4
4 1
4 2
4 3
4 4
'🌝Coding > 🌟Python3' 카테고리의 다른 글
[Python] heapq (0) | 2021.08.20 |
---|---|
[Python] lambda (0) | 2021.07.29 |
[Python] 딕셔너리 (0) | 2021.05.02 |
[가상환경] 파이썬 환경설정 (0) | 2021.03.13 |
[Python] list (0) | 2021.02.09 |