์๊ณ ๋ฆฌ์ฆ ๋ฌธ์ ๋ฅผ ํ ๋, ์์ด๊ณผ ์กฐํฉ ๊ฐ๋ ์ด ํ์ํ ๋ฌธ์ ๋ค์ด ์๋ค.
itertools ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ๋ฉด ๋งค์ฐ ๊ฐ๋จํ๊ฒ ์์ด๊ณผ ์กฐํฉ์ ๋ง๋ค ์ ์๋ค.
itertools
ํ์ด์ฌ์ ์๋ ํ์ค ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ค.
๋ค์ํ iterator๊ฐ ์์ง๋ง ๊ทธ์ค permutations(), combinations()๋ง ๊ฐ๋จํ๊ฒ ๋ค๋ฃฌ๋ค.
permutations()
from itertools import permutations
arr = [1, 2, 3, 4]
for p in permutations(arr, 2):
print(p)
๊ฒฐ๊ณผ:
(1, 2)
(1, 3)
(1, 4)
(2, 1)
(2, 3)
(2, 4)
(3, 1)
(3, 2)
(3, 4)
(4, 1)
(4, 2)
(4, 3)
tuple ํํ๋ก ๋์จ๋ค.
combinations()
from itertools import combinations
arr = [1, 2, 3, 4]
for c in combinations(arr,3):
print(c)
๊ฒฐ๊ณผ:
(1, 2, 3)
(1, 2, 4)
(1, 3, 4)
(2, 3, 4)
'๐ Language > [python]' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ์ด์ฌ] ํ์ด์ฌ ์ฝ๋ ์คํ ์ธก์ - time (0) | 2022.08.05 |
---|---|
[ํ์ด์ฌ] if __name__ == "__main__" (0) | 2022.08.03 |
[ํ์ด์ฌ] sort (feat. lambda) (0) | 2022.08.02 |
[ํ์ด์ฌ] 0100110๊ณผ ๊ฐ์ด 0์ด ๋ถ์ด์ ์ ๋ ฅ๋ ๋ (0) | 2022.08.02 |
[ํ์ด์ฌ] 2์ฐจ์ ๋ฆฌ์คํธ ๋ณต์ฌํ ๋ - deep copy, slicing (0) | 2022.08.02 |