Potato
์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์žก๋‹ˆ๋‹ค?๐Ÿฅ” ^___^ ๐Ÿ˜บ github ๋ฐ”๋กœ๊ฐ€๊ธฐ ๐Ÿ‘‰๐Ÿป

potato's Dev Note/Python

[python3] itertools ์ดํ„ฐ๋ ˆ์ดํ„ฐ๋ฅผ ๋งŒ๋“œ๋Š” ํ•จ์ˆ˜ ์ข…๋ฅ˜ ์‚ดํŽด๋ณด๊ธฐ

๊ฐ์ž ๐Ÿฅ” 2021. 8. 4. 23:55
๋ฐ˜์‘ํ˜•

 

1. Itertools

  • ํŒŒ์ด์ฌ์—์„œ ์ œ๊ณตํ•˜๋Š” ์ž์‹ ๋งŒ์˜ ๋ฐ˜๋ณต์ž๋ฅผ ์ƒ์„ฑํ•ด์ฃผ๋Š” ํ•จ์ˆ˜์ด๋‹ค.
  • ํŠน์ • ๋ฐฐ์—ด์ด๋‚˜ ์ˆœ์—ด์— ๋Œ€ํ•œ ์กฐํ•ฉ์„ ๋งŒ๋“ค์–ด์„œ ์ด๋ฅผ ์ด์šฉํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ๋ฅผ ํ’€๋•Œ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉ๋œ๋‹ค.
  • ํšจ์œจ์ ์ธ ๋ฐ˜๋ณต์ž๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๋ฌดํ•œ ์ดํ„ฐ๋ ˆ์ดํ„ฐ / ์กฐํ•ฉํ˜• ์ดํ„ฐ๋ ˆ์ดํ„ฐ / ์ข…๋ฃŒ ์ดํ„ฐ๋ ˆ์ดํ„ฐ ๊ฐ€ ์žˆ์ง€๋งŒ, ์ข…๋ฃŒ ์ดํ„ฐ๋ ˆ์ดํ„ฐ๋Š” ๋„ˆ๋ฌด ๋งŽ์œผ๋‹ˆ ์•„๋ž˜ ๋งํฌ๋กœ ์ฒจ๋ถ€ํ•ด๋‘๊ณ , ์ž์ฃผ ์‚ฌ์šฉํ•  ๊ฒƒ ๊ฐ™์€ ์ดํ„ฐ๋ ˆ์ดํ„ฐ๋งŒ ์ž‘์„ฑํ•  ๊ฒƒ์ด๋‹ค.

 

2. ๋ฌดํ•œ ์ดํ„ฐ๋ ˆ์ดํ„ฐ

 2.1 Count()

from itertools import count
a=count(10,10)

#-------ํ˜ธ์ถœ----------------------
next(a)
#๊ฒฐ๊ณผ : 10

next(a)
#๊ฒฐ๊ณผ : 20 
.
.
.
  • count(์‹œ์ž‘์ˆซ์ž, ๋”ํ•ด์ค„์ˆซ์ž) ์ž…๋ ฅํ•ด์ฃผ๋ฉด ๋ฌดํ•œ์œผ๋กœ ๊ณ„์† ๊ทœ์น™์— ๋งž๊ฒŒ ํ˜ธ์ถœ๋œ๋‹ค.

 

2.2 Cycle()

from itertools import cycle
a = cycle([1,2,3,4,5])

#--------๊ฒฐ๊ณผํ˜ธ์ถœ------------
next(a)
# ๊ฒฐ๊ณผ : 1

next(a)
# ๊ฒฐ๊ณผ : 2
  • a ์˜ cycle์— ์ž…๋ ฅ๋œ ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋ฐ˜๋ณต๋˜์–ด cycle์— ๋“ค์–ด๊ฐ„๋‹ค. ๋”ฐ๋ผ์„œ 5 ๋‹ค์Œ์˜ next๋Š” 1๋กœ ๋‹ค์‹œ ์ฒ˜์Œ์œผ๋กœ ๋Œ์•„๊ฐ€๋Š” ๋ฌดํ•œ ๋ฐ˜๋ณต ์‚ฌ์ดํด์„ ์ƒ์„ฑํ•œ๋‹ค.

 

2.3 repeat()

from itertools import repeat
a = repeat("AAA", 4)
print(next(a))
print(next(a))
print(next(a))
print(next(a))
print(next(a))

  • repeat(๋ฐ˜๋ณตํ• ๊ฒƒ, ๋ฐ˜๋ณตํšŸ์ˆ˜) ๋กœ ์ง€์ •
  • ์œ„์˜ ์˜ˆ์‹œ์—์„œ๋Š” 4๋ฒˆ ์˜ AAA๋ฅผ ์ถœ๋ ฅํ•œ ํ›„, ๋‹ค์„ฏ๋ฒˆ์งธ๋Š” ์ถœ๋ ฅ์„ ๋ชปํ•˜๊ณ  ์˜ค๋ฅ˜๊ฐ€ ๋‚˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

 

3. ์กฐํ•ฉ ์ดํ„ฐ๋ ˆ์ดํ„ฐ

3.1 product()

from itertools import product

a = ["a", "b", "c"]
pd = list(product(a, repeat = 3))

  • product(list, repeat = n) repeat ๋ถ€๋ถ„์—๋Š” ๋ช‡๊ฐœ ์”ฉ ์กฐํ•ฉํ• ๊ฑด์ง€ ์“ฐ๋Š” ๊ฑฐ๋ผ๊ณ  ์ดํ•ดํ•˜๋ฉด ๋œ๋‹ค.
  • ๋ฐ์นด๋ฅดํŠธ์˜ ๊ณฑ . ์ค‘์ฒฉ๋œ for๋ฃจํ”„์™€ ๋™์ผ
  • ๋‘ ๊ฐœ ์ด์ƒ์˜ ๋ฆฌ์ŠคํŠธ์˜ ๋ชจ๋“  ์กฐํ•ฉ์„ ๊ตฌํ•  ๋•Œ ์ฃผ๋กœ ์‚ฌ์šฉ๋œ๋‹ค.
  • ๋ฆฌ์ŠคํŠธ a ์— ์žˆ๋Š” ๋ชจ๋“  ์ˆ˜์˜ ์กฐํ•ฉ ๋ฐฉ๋ฒ•์ด pd์— ๋“ค์–ด๊ฐ”๋‹ค. 
  • ์ค‘๋ณต์ด ํ—ˆ์šฉ๋˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜๊ฐ€ ์žˆ๋‹ค. 
  • len(pd) ๋Š” 27์ด ๋‚˜์˜จ๋‹ค (3๊ฐœ๋ฅผ ์ค‘๋ณต์„ ํ—ˆ์šฉํ•œ ์ฑ„ ์กฐํ•ฉํ•ด์„œ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜)
st = list(product('ABCD', repeat=2))
st

  • ๋ฆฌ์ŠคํŠธ์ด๋˜, strํ˜•ํƒœ์ด๋˜ ์ƒ๊ด€์—†๋‹ค.
# ๋‘ ๊ฐœ ์ด์ƒ์˜ ์กฐํ•ฉ์„ ๊ตฌํ•  ๋•Œ
from itertools import product

a = ["a", "b", "c"]
b = [1,2,3]

pd = list(product(a, b, repeat=1))

 

3.2 permutations()

from itertools import permutations

a = [1, 2, 3]
permut = list(permutations(a, 3))
print(permut)

  • ํ•œ ๋ฆฌ์ŠคํŠธ ๋‚ด ์—์„œ๋งŒ ์ค‘๋ณต์„ ํ—ˆ์šฉํ•œ ์ฑ„๋กœ ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•œ๋‹ค.
  • ์ˆœ์—ด์„ ํ‘œํ˜„ํ•  ๋•Œ ์‚ฌ์šฉ๋˜๋Š” ๋ฉ”์„œ๋“œ ์ด๋‹ค.

 

3.3 combinations()

from itertools import combinations

a = [1, 2, 3]
combi = list(combinations(a, 2))
print(combi)

  • ์กฐํ•ฉ์„ ๊ตฌํ•˜๋Š” ๋ฉ”์„œ๋“œ ์ด๋‹ค.
  • ๋ฆฌ์ŠคํŠธ์—์„œ ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๊ณ  ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•œ๋‹ค.
# ๊ฐฏ์ˆ˜ ๋ณ„๋กœ ์กฐํ•ฉ์„ ๋ฐ˜๋ณตํ•  ์ˆ˜ ์žˆ๋‹ค.
a = [1, 2, 3]
for i in range(1, len(a) + 1):
    print(list(combinations(a, i)))

  • ๋ฐ”๋กœ ์œ„์— ์ฒ˜๋Ÿผ for๋ฌธ ์•ˆ์— ๋„ฃ๊ณ , ๊ฐฏ์ˆ˜๋ณ„๋กœ ์กฐํ•ฉ์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

3.4 combinations_with_replacement()

combinations๊ณผ ๋™์ผํ•œ๋ฐ, ์ž๊ธฐ์ž์‹ ๊ณผ์˜ ์กฐํ•ฉ๊นŒ์ง€ ์ถœ๋ ฅํ•ด์ค€๋‹ค. ์•„๋ž˜ ๋‘ ๋ฉ”์„œ๋“œ์˜ ์ฐจ์ด์ ์„ ๋ณด๋ฉด ์ดํ•ด๊ฐ€ ๋  ๊ฒƒ์ด๋‹ค.

https://docs.python.org/ko/3.8/library/itertools.html#itertools.count

 

 

์ฐธ๊ณ ์ž๋ฃŒ

https://docs.python.org/ko/3.8/library/itertools.html#itertools.count

 

itertools — ํšจ์œจ์ ์ธ ๋ฃจํ•‘์„ ์œ„ํ•œ ์ดํ„ฐ๋ ˆ์ดํ„ฐ๋ฅผ ๋งŒ๋“œ๋Š” ํ•จ์ˆ˜ — Python 3.8.11 ๋ฌธ์„œ

 

docs.python.org

https://velog.io/@davkim1030/Python-%EC%88%9C%EC%97%B4-%EC%A1%B0%ED%95%A9-product-itertools

 

๋ฐ˜์‘ํ˜•