๊ด€๋ฆฌ ๋ฉ”๋‰ด

IT’s Portfolio

[Python] Iterator ๋ณธ๋ฌธ

Development Study/Python

[Python] Iterator

f1r3_r41n 2022. 11. 28. 15:22
728x90
๋ฐ˜์‘ํ˜•

๐Ÿ’ป Iterator

๐Ÿค” What is Iterator?

  • ๋ฐ˜๋ณต์ž: ์ˆœ์„œ๋Œ€๋กœ ๋‹ค์Œ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ์ฒด๋ฅผ ์˜๋ฏธ
  • ์ž์ฒด ๋‚ด์žฅ๋œ next ๋งค์ง ๋ฉ”์„œ๋“œ

Iterator ๊ฐ์ฒด ๊ตฌํ˜„

class Class_of_Iteration:
    def __init__(self, start_num, stop_num):
        self.pointer = start_num
        self.max = stop_num

    def __iter__(self):
        return self

    def __next__(self):
        if self.pointer > self.max:
            raise StopIteration
        else:
            self.pointer+=1
            return self.pointer-1

A = Class_of_Iteration(1, 3)
print(type(A))
print(A.__next__())
print(A.__next__())
print(A.__next__())
print(A.__next__()) # ์˜ˆ์™ธ๋ฐœ์ƒ: StopIteration

๐Ÿ‘‰๐Ÿป Collection Type & Sequence Type

  • Collection: list, tuple, set, dict์™€ ๊ฐ™์ด ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์š”์†Œ๋ฅผ ๊ฐ–๋Š” ๋ฐ์ดํ„ฐ ํƒ€์ž…
  • Sequence: list, tuple, range, str๊ณผ ๊ฐ™์ด ์ˆœ์„œ๊ฐ€ ์กด์žฌํ•˜๋Š” ๋ฐ์ดํ„ฐ ํƒ€์ž…

๐Ÿ‘‰๐Ÿป Iterable & Iterator

  • ๋‚ด๋ถ€ ์š”์†Œ๋“ค์„ ํ•˜๋‚˜์”ฉ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ์ฒด๋ฅผ Iterable (๋ฐ˜๋ณต ๊ฐ€๋Šฅ)ํ•˜๋‹ค๊ณ  ํ•จ
  • for๋ฌธ์„ ํ†ตํ•ด ์ˆœํšŒํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ์ฒด
  • Iterable์€ next X
  • Iterator์€ next O
  • Iterableํ•œ ๊ฐ์ฒด๋Š” Iterator๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Œ
    • iter() ์‚ฌ์šฉ
      • iter() ์— ๋Œ€ํ•œ ๋ถ€๊ฐ€์ ์ธ ์„ค๋ช…
        • iter(object to callable, sentinel)
        • ๋ฐ˜๋ณต์„ ๋๋‚ผ ๊ฐ’์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Œ
    • .__iter__() ์‚ฌ์šฉ
  • Iterator์€ ๋‘ ํ•จ์ˆ˜๋กœ ๋‹ค์Œ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ์Œ
    • next()
    • .__next__()

๐Ÿ‘‰๐Ÿป Loop & Iterator

  • Loop๋Š” ์‹คํ–‰๊ณผ ๋™์‹œ์— ๊ฐ’์„ ๊ณ„์† ๋งŒ๋“ค์–ด๋‚ด๋ฉฐ ๋ฉ”๋ชจ๋ฆฌ ์ฐจ์ง€
  • Iterator์€ ์ˆœ์„œ๋Œ€๋กœ ๋‹ค์Œ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ๋Š” ์ˆœํšŒ ๊ฐ€๋Šฅํ•œ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ ์ˆ˜ ํ•„์š”ํ•œ ์‹œ์ ์— next ํ•จ์ˆ˜๋กœ ์ž‘๋™ํ•˜๊ธฐ์— ํšจ์œจ์„ฑ์ด ์ข‹์Œ
    • ๋ฐ์ดํ„ฐ ์ƒ์„ฑ์„ ๋ฏธ๋ฃจ๋Š” ๋ฐฉ์‹์„ lazyevaluation ์ด๋ผ๊ณ  ์นญํ•จ
  • ํŒŒ์ด์ฌ for๋ฌธ์€ ๋‚ด๋ถ€์ ์œผ๋กœ Iterator๋ฅผ ์ƒ์„ฑํ•ด ๋™์ž‘
    • ํ•ด๋‹น ๊ฐ์ฒด์˜ iter๋ฅผ ์ƒ์„ฑํ•ด next๋กœ ์ˆœํšŒ
    • StopIteration ๋ฐœ์ƒ ์‹œ ์ˆœํšŒ ์ค‘๋‹จ
  • Iterator์€ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์•ผํ•˜๋Š” while๋ณด๋‹ค๋Š” StopIteration์„ ์‰ฝ๊ฒŒ ์ œ์–ดํ•  ์ˆ˜ ์žˆ๋Š” for์™€ ์ž์ฃผ ์“ฐ์ž„

Iterator Control 1

# while
import random

while_res = []

while 1:
    i = random.randint(0, 10)
    if i == 4:
        break
    else:
        while_res.append(i)

print(while_res)

# iterator
# lambda ๋งค๊ฐœ๋ณ€์ˆ˜ : ํ‘œํ˜„์‹
iter_res = [x for x in iter(lambda: random.randint(0, 10), 4)]
print(iter_res)
  • random module์„ ์‚ฌ์šฉํ•œ ์ฝ”๋“œ

Iterator Control 2

N = ['f1r3', '_', 'r41n']
# while
while_iter = iter(N)
while 1:
    try:
        i = next(while_iter)
    except StopIteration:
        break
    print(i, end='')

# for
for_iter = iter(N)
for _ in range(len(N)):
    print(for_iter.__next__(), end='')
  • while๊ณผ for์ด StopIterator์„ ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌํ•˜๋Š”์ง€์— ๋Œ€ํ•œ ์ฝ”๋“œ
728x90
๋ฐ˜์‘ํ˜•
Comments