๐ŸŒCoding/๐ŸŒŸPython3

[Python] ์ˆœ์—ด, ์กฐํ•ฉ

๋ฟŒ์•ผ._. 2021. 5. 16. 13:59

๐Ÿ˜‰ ์กฐํ•ฉ

 

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