๐ ์กฐํฉ
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 |