permutations (์์ด)
ํ์ด์ฌ์ permutations ํจ์๋ ์์ด์ ์์ฑํ์ฌ, ์ฃผ์ด์ง ์์๋ก ๋ฝ์ ์ ์๋ ๋ชจ๋ ๊ฒฝ์ฐ์ ์๋ฅผ ๊ตฌํ๋ค. ์์๊ฐ ์ค์ํ ๊ฒฝ์ฐ์ ์ฌ์ฉ๋๋ค. itertools ๋ชจ๋์ ํฌํจ๋์ด ์์ด ์ฌ์ฉ ์ ์ ์ด ๋ชจ๋์ ์ํฌํธํด์ผ ํ๋ค.
๊ธฐ๋ณธ ๊ตฌ๋ฌธ
from itertools import permutations
permutations(iterable, r)
- iterable : ์์ด์ ์์ฑํ ๋์ (๋ฆฌ์คํธ, ๋ฌธ์์ด ๋ฑ).
- r : ์์ด์ ๊ธธ์ด (๋ฝ์ ๊ฐ์). r์ ์๋ตํ๋ฉด ๊ธฐ๋ณธ๊ฐ์ len(iterable)๋ก ์ค์ ๋๋ค.
์ฌ์ฉ ๋ฐฉ๋ฒ
1๏ธโฃ ๊ธฐ๋ณธ ์ฌ์ฉ
from itertools import permutations
items = ['a', 'b', 'c']
perm = permutations(items, 2)
print(list(perm))
[('a', 'b'), ('a', 'c'), ('b', 'a'), ('b', 'c'), ('c', 'a'), ('c', 'b')]
- items์์ 2๊ฐ์ฉ ์์ ์๊ฒ ๋ฝ๋ ๋ชจ๋ ๊ฒฝ์ฐ์ ์๋ฅผ ์์ฑํ๋ค.
- ๊ฒฐ๊ณผ๋ ํํ ํํ๋ก ๋ฐํ๋๋ค.
2๏ธโฃ ์ ์ฒด ๊ธธ์ด์ ์์ด ์์ฑ
r ๊ฐ์ ์๋ตํ๋ฉด len(iterable)์ ์ฌ์ฉํ์ฌ ์ ์ฒด ๊ธธ์ด์ ์์ด์ ์์ฑํ๋ค.
perm = permutations(items)
print(list(perm))
์ถ๋ ฅ ๊ฒฐ๊ณผ
[('a', 'b', 'c'), ('a', 'c', 'b'), ('b', 'a', 'c'), ('b', 'c', 'a'), ('c', 'a', 'b'), ('c', 'b', 'a')]
3๏ธโฃ ๋ฌธ์์ด ์์ด ์์ฑ
๋ฌธ์์ด์์๋ ์ฌ์ฉํ ์ ์๋ค. ๊ฐ ๋ฌธ์ ์์๊ฐ ์ค์ํ ๋ชจ๋ ๊ฒฝ์ฐ์ ์๋ฅผ ์์ฑํ๋ค.
perm = permutations('ABC', 2)
print(list(perm))
์ถ๋ ฅ ๊ฒฐ๊ณผ
[('A', 'B'), ('A', 'C'), ('B', 'A'), ('B', 'C'), ('C', 'A'), ('C', 'B')]
4๏ธโฃ ์ค์ฒฉ for๋ฌธ์ผ๋ก ์ถ๋ ฅํ๊ธฐ
permutations์ ๊ฒฐ๊ณผ๋ฅผ ์ค์ฒฉ๋ for๋ฌธ์ผ๋ก ์ฒ๋ฆฌํด ์ํ๋ ํํ๋ก ์ถ๋ ฅํ ์ ์๋ค.
items = [1, 2, 3]
for perm in permutations(items, 2):
print(perm)
์ถ๋ ฅ ๊ฒฐ๊ณผ
(1, 2)
(1, 3)
(2, 1)
(2, 3)
(3, 1)
(3, 2)
์์ฝ
- permutations๋ ์์๋ฅผ ๊ณ ๋ คํ์ฌ ํน์ ๊ธธ์ด๋งํผ ์์๋ฅผ ๋ฝ๋ ๋ชจ๋ ์์ด์ ์์ฑํ๋ค.
- ๊ฒฐ๊ณผ๋ ํํ๋ก ๋ฐํ๋๋ฉฐ, list()๋ก ๋ณํํด ๋ชจ๋ ์์ด์ ๋ฆฌ์คํธ๋ก ๋ณผ ์ ์๋ค.
- ์์ด์ ๊ธธ์ด r์ ์ง์ ํ ์ ์์ผ๋ฉฐ, ์๋ต ์ ์ ์ฒด ๊ธธ์ด๋ก ์์ด์ด ์์ฑ๋๋ค.
- itertools ๋ชจ๋์์ ๊ฐ์ ธ์์ผ ์ฌ์ฉ ๊ฐ๋ฅํ๋ค.
combinations (์กฐํฉ)
ํ์ด์ฌ์ combinations๋ ์์์ ์๊ด์์ด ์ฃผ์ด์ง ๊ธธ์ด๋งํผ ์์๋ฅผ ๋ฝ๋ ๋ชจ๋ ์กฐํฉ์ ์์ฑํด์ฃผ๋ ํจ์์ด๋ค. itertools ๋ชจ๋์ ํฌํจ๋์ด ์์ด, ์ด ๋ชจ๋์ ๋จผ์ ์ํฌํธํด์ผ ํ๋ค.
๊ธฐ๋ณธ ๊ตฌ๋ฌธ
from itertools import combinations
combinations(iterable, r)
- iterable : ์กฐํฉ์ ์์ฑํ ๋์(์: ๋ฆฌ์คํธ, ๋ฌธ์์ด ๋ฑ).
- r : ์กฐํฉ์ ๊ธธ์ด (๋ฝ์ ๊ฐ์).
์ฌ์ฉ ๋ฐฉ๋ฒ
1๏ธโฃ ๊ธฐ๋ณธ ์ฌ์ฉ
from itertools import combinations
items = ['a', 'b', 'c']
combi = combinations(items, 2)
print(list(combi))
์ถ๋ ฅ ๊ฒฐ๊ณผ
[('a', 'b'), ('a', 'c'), ('b', 'c')]
- 'a', 'b', 'c' ์ค์์ 2๊ฐ์ฉ ์กฐํฉ์ ์์ฑํ๋ค.
- ๊ฒฐ๊ณผ๋ ํํ ํํ๋ก ๋ฐํ๋๋ค.
2๏ธโฃ ๋ฌธ์์ด์์ ์กฐํฉ ์์ฑ
๋ฌธ์์ด๋ iterable์ด๊ธฐ ๋๋ฌธ์, ์กฐํฉ์ ๊ตฌํ ์ ์๋ค.
combi = combinations('ABCD', 2)
print(list(combi))
์ถ๋ ฅ ๊ฒฐ๊ณผ
[('A', 'B'), ('A', 'C'), ('A', 'D'), ('B', 'C'), ('B', 'D'), ('C', 'D')]
3๏ธโฃ ๋ชจ๋ ์กฐํฉ ์์ฑ
์ ์ฒด ๊ฐ๋ฅํ ์กฐํฉ์ ๊ตฌํ๋ ค๋ฉด, ๊ธธ์ด r์ 1๋ถํฐ len(iterable)๊น์ง ๋๋ ค๊ฐ๋ฉด์ ๊ตฌํ ์ ์๋ค.
items = ['x', 'y', 'z']
all_combinations = [list(combinations(items, r)) for r in range(1, len(items) + 1)]
print(all_combinations)
์ถ๋ ฅ ๊ฒฐ๊ณผ
[[('x',), ('y',), ('z',)], [('x', 'y'), ('x', 'z'), ('y', 'z')], [('x', 'y', 'z')]]
4๏ธโฃ ์ด์ค for๋ฌธ์ผ๋ก ์ถ๋ ฅํ๊ธฐ
combinations๋ฅผ ์ด์ค for๋ฌธ์ผ๋ก ์ํํ์ฌ ์ํ๋ ํํ๋ก ์ถ๋ ฅํ ์๋ ์๋ค.
items = [1, 2, 3]
for combi in combinations(items, 2):
print(combi)
์ถ๋ ฅ ๊ฒฐ๊ณผ
(1, 2)
(1, 3)
(2, 3)
์์ฝ
- combinations๋ ์์๋ฅผ ๊ณ ๋ คํ์ง ์๊ณ ํน์ ๊ธธ์ด๋งํผ ์์๋ฅผ ๋ฝ๋ ๋ชจ๋ ์กฐํฉ์ ์์ฑํ๋ค.
- ๊ฒฐ๊ณผ๋ ํํ ํํ๋ก ๋ฐํ๋๋ฉฐ, list()๋ก ๋ณํํ๋ฉด ์ ์ฒด ์กฐํฉ์ ๋ฆฌ์คํธ๋ก ๋ณผ ์ ์๋ค.
- ์กฐํฉ์ ๊ธธ์ด r์ ์ง์ ํด์ผ ํ๋ฉฐ, itertools ๋ชจ๋์์ ๊ฐ์ ธ์์ผ ์ฌ์ฉ ๊ฐ๋ฅํ๋ค.
Permutations ์ Combinations ์ ์ฐจ์ด
permutations์ combinations์ ์ฃผ์ ์ฐจ์ด๋ ์์๋ฅผ ๊ณ ๋ คํ๋์ง ์ฌ๋ถ์ด๋ค.
- permutations
- ์์๋ฅผ ์ค์ํ๊ฒ ์๊ฐํ๋ค. ๊ฐ์ ์์๋ผ๋ ์์๊ฐ ๋ค๋ฅด๋ฉด ๋ค๋ฅธ ๊ฒฝ์ฐ๋ก ๊ฐ์ฃผํ๋ค.
- ์๋ฅผ ๋ค์ด, ('a', 'b')์ ('b', 'a')๋ ๋ค๋ฅธ ์์ด๋ก ๋ณธ๋ค.
- combinations
- ์์๋ฅผ ์ค์ํ์ง ์๊ฒ ์๊ฐํ๋ค. ๊ฐ์ ์์๊ฐ ๋ค๋ฅธ ์์๋ก ๋ฐฐ์น๋๋๋ผ๋ ๊ฐ์ ์กฐํฉ์ผ๋ก ๊ฐ์ฃผํ๋ค.
- ์๋ฅผ ๋ค์ด, ('a', 'b')์ ('b', 'a')๋ ๊ฐ์ ์กฐํฉ์ผ๋ก ์ทจ๊ธ๋๋ค.
์์
- permutations(['a', 'b'], 2) → [('a', 'b'), ('b', 'a')]
- combinations(['a', 'b'], 2) → [('a', 'b')]
๐ ์ ๋ฆฌ
permutations๋ ์์๊ฐ ์ค์ํ ๋ ์ฌ์ฉํ๊ณ , combinations๋ ์์๊ฐ ์ค์ํ์ง ์์ ๋ ์ฌ์ฉํ๋ค.
'๐ฌ Explore > ํ์ด์ฌ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [ํ์ด์ฌ] ์ด์ง๋ฒ, ์ด์ง์, 2์ง์ ๋ณํ ๋ฐฉ๋ฒ (0) | 2024.11.15 |
|---|---|
| [ํ์ด์ฌ] lambda(๋๋ค) ํจ์ (0) | 2024.10.30 |
| [ํ์ด์ฌ] enumerate() ํจ์ (0) | 2024.10.29 |
| [ํ์ด์ฌ] set, ์งํฉ ํจ์ (0) | 2024.10.29 |
| [ํ์ด์ฌ] zip() ํจ์ (0) | 2024.10.25 |