ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

728x90
๋ฐ˜์‘ํ˜•

permutations

from itertools import permutations
nums = [num + 1 for num in range(3)]

for cases in permutations(nums, 2):
    print(cases, end=' ')
  • permutations(iterater, reapeat)๋ฅผ ํ†ตํ•ด ์ˆœ์—ด์„ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๊ฒฐ๊ณผ : (1, 2) (1, 3) (2, 1) (2, 3) (3, 1) (3, 2), ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ์ˆœ์„œ๋กœ ๋ฐ˜๋ณต์„ ํ•˜์ง€ ์•Š์Œ

combinations

from itertools import combinations
nums = [num + 1 for num in range(3)]

for cases in combinations(nums, 2):
    print(cases, end=' ')
  • combinations(iterator, repeat)๋ฅผ ํ†ตํ•ด ์ˆœ์—ด์„ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๊ฒฐ๊ณผ : (1, 2) (1, 3) (2, 3), ์ •๋ ฌ๋œ ์ˆœ์„œ๋กœ ๋ฐ˜๋ณต์„ ํ•˜์ง€ ์•Š์Œ

product

from itertools import product
nums = [num + 1 for num in range(3)]

for cases in product(nums, repeat=2):
    print(cases, end=' ')
  • product(iterator, iterator) ๋˜๋Š” product(iteratore, repeat=)๋กœ ์ˆœ์—ด์„ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๊ฒฐ๊ณผ : (1, 1) (1, 2) (1, 3) (2, 1) (2, 2) (2, 3) (3, 1) (3, 2) (3, 3), permuation์—์„œ (1, 1), (2, 2), (3, 3)์ด ์ถ”๊ฐ€๋œ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ์ฆ‰ ๋ฐ์นด๋ฅดํŠธ์˜ ๊ณฑ์ด๋‹ค.

combinations_with_replacement

from itertools import combinations_with_replacement
nums = [num + 1 for num in range(3)]

for cases in combinations_with_replacement(nums, 2):
    print(cases, end=' ')
  • combinations_with_replacement(iterator, num) ์œผ๋กœ ์ˆœ์—ด์„ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๊ฒฐ๊ณผ : (1, 1) (1, 2) (1, 3) (2, 2) (2, 3) (3, 3), combination์—์„œ (1, 1), (2, 2), (3, 3)์ด ์ถ”๊ฐ€๋œ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.
728x90
๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€
๊ธ€ ๋ณด๊ด€ํ•จ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€