ํฐ์คํ ๋ฆฌ ๋ทฐ
๐โ๏ธ ํ๋ก๊ทธ๋๋ฐ ์ธ์ด/Python
Python: itertools ํ์ฉํ๊ธฐ
dirmathfl 2020. 6. 6. 13:02728x90
๋ฐ์ํ
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
๋ฐ์ํ
'๐โโ๏ธ ํ๋ก๊ทธ๋๋ฐ ์ธ์ด > Python' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Python: sort, sorted ํ์ฉํ๊ธฐ (0) | 2020.09.23 |
---|---|
Python: ๋ฌธ์ ์ ๊ทผ ์๋๋ฅผ ๋์ฌ์ฃผ๋ ์ฝ๋๋ค (0) | 2020.06.07 |
Python: collections ํ์ฉํ๊ธฐ (0) | 2020.06.05 |
Python: heapq ํ์ฉํ๊ธฐ (0) | 2020.06.04 |
Python: List ํ์ฉํ๊ธฐ (0) | 2020.06.03 |
๋๊ธ
๊ธ ๋ณด๊ดํจ
์ต๊ทผ์ ์ฌ๋ผ์จ ๊ธ
์ต๊ทผ์ ๋ฌ๋ฆฐ ๋๊ธ