๐Ÿ—‚ Iteratable & Iterator

Min-Ho Leeยท2020๋…„ 6์›” 30์ผ
0

Learn Python

๋ชฉ๋ก ๋ณด๊ธฐ
18/22
post-thumbnail

Wednesday_July_1
.
.
.

๐Ÿ—‚ Iterator

๋ธ”๋กœ๊ทธ์— ์ •๋ฆฌ๋ฅผ ํ•˜๋ ค๋Š”๋ฐ ์ค‘์š”ํ•œ ์‚ฌ์‹ค์„ ์•Œ์•˜๋‹ค.

Iterable๊ณผ Iterator๋Š” ๋‹ค๋ฅด๋‹ค

โ“ Iterable

์š”์†Œ๋“ค์„ ํ•˜๋‚˜์”ฉ ์ฐจ๋ก€๋กœ ๋ฐ˜ํ™˜์ด ๊ฐ€๋Šฅํ•œ Object๋ฅผ ๋œปํ•œ๋‹ค.
๋Œ€ํ‘œ์ ์ธ ์˜ˆ๋กœ Sequence type์ธ list, tuple, str๋“ฑ์ด ์žˆ๋‹ค.

โ“ Iterator

Iterator๋Š” next()๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ˆœ์ฐจ์ ์œผ๋กœ ํ˜ธ์ถœ์ด ๊ฐ€๋Šฅํ•œ object์ด๋‹ค. ๋งŒ์•ฝ next()๋กœ ๋‹ค์Œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์—†์„ ๊ฒฝ์šฐ, StopIteration Exception ์„ ๋ฐœ์ƒ์‹œํ‚จ๋‹ค.

์—ฌ๊ธฐ์„œ ๊ถ๊ธˆํ–ˆ๋˜๊ฒŒ Iterableํ•œ Object๋“ค์€ ๋‹ค Iterator์ธ๊ฐ€??

๊ฒฐ๋ก ๋ถ€ํ„ฐ ๋งํ•˜์ž๋ฉด Iterable์ด๋ผ๊ณ  ๋ฐ˜๋“œ์‹œ Iterator๋Š” ์•„๋‹ˆ๋‹ค.

L = [1, 2, 3]
print(next(L))

์ถœ๋ ฅ ๊ฒฐ๊ณผ

TypeError: 'list' object is not an iterator

L์ด๋ผ๋Š” list๋Š” Iterableํ•œ ๊ฐ์ฒด์ธ๋ฐ, list๋Š” Iteratorํƒ€์ž…์ด ์•„๋‹ˆ๋ผ๋Š” ์—๋Ÿฌ๊ฐ€ ๋œฌ๋‹ค.

์•„๊นŒ ๋งํ–ˆ๋“ฏ, Iterator๋Š” next()๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ˆœ์ฐจ์ ์œผ๋กœ ํ˜ธ์ถœ์ด ๊ฐ€๋Šฅํ•œ ๊ฐ์ฒด๋ฅผ ๋งํ•œ๋‹ค.

์—ฌ๊ธฐ์„œ Iterable ๊ฐ์ฒด๋“ค์„ nextํ•จ์ˆ˜๋กœ ํ˜ธ์ถœ์ด ๊ฐ€๋Šฅํ•œ Iterator ๊ฐ์ฒด๋“ค๋กœ ๋ฐ”๊ฟ”์ฃผ๊ณ  ์‹ถ๋‹ค๋ฉด, iter()๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

์ฐจ์ด์ ์„ ์ฝ”๋“œ๋กœ ๋ณด์—ฌ์ฃผ์ž๋ฉด,

L = [1, 2, 3]
print(type(L))
print(type(iter(L)))

์ถœ๋ ฅ ๊ฒฐ๊ณผ

<class 'list'>
<class 'list_iterator'>

L์ด๋ผ๋Š” list์— 1,2,3 ์ด๋ผ๋Š” ์š”์†Œ๋ฅผ ์ฃผ๊ณ , ๊ฐ ์š”์†Œ์˜ ์ œ๊ณฑ์„ ์ถœ๋ ฅํ•˜๋Š” ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

L = [1, 2, 3]

for x in L:
    print(x ** 2)

์—ฌ๊ธฐ์„œ L์ด๋ผ๋Š” list๊ฐ€ ๋ฐ˜๋ณต์ด ๊ฐ€๋Šฅํ•œ ๊ฐ์ฒด์ธ์ง€ ์–ด๋–ป๊ฒŒ ์•Œ๊นŒ?

dir() ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์•ˆ์— (๋”๋ธ”์–ธ๋”๋ฐ”)iter(๋”๋ธ”์–ธ๋”๋ฐ”) ํ•จ์ˆ˜๊ฐ€ ์žˆ๋Š”์ง€ ๋ณด๋Š” ๊ฒƒ์ด๋‹ค.

์—ฌ๊ธฐ์„œ dir() ํ•จ์ˆ˜๋Š” '์–ด๋– ํ•œ ๊ฐ์ฒด๋ฅผ ์ธ์ž๋กœ ๋„ฃ์–ด์ฃผ๋ฉด ํ•ด๋‹น ๊ฐ์ฒด๊ฐ€ ์–ด๋–ค ๋ณ€์ˆ˜์™€ ๋ฉ”์†Œ๋“œ๋ฅผ ๊ฐ–๊ณ  ์žˆ๋Š”์ง€๋ฅผ ๋‚˜์—ดํ•ด์ฃผ๋Š” ํ•จ์ˆ˜'์ด๋‹ค. ๊ทธ๋ฆฌ๊ณ  (๋”๋ธ”์–ธ๋”๋ฐ”)iter(๋”๋ธ”์–ธ๋”๋ฐ”)๋ผ๋Š” ๋ฉ”์†Œ๋“œ๊ฐ€ ์กด์žฌ ํ•œ๋‹ค๋Š” ๊ฒƒ์€ ๊ทธ ๊ฐ์ฒด๊ฐ€ ๋ฐ˜๋ณต์ด ๊ฐ€๋Šฅํ•œ ๊ฐ์ฒด๋ผ๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค.

L = [1, 2, 3]
print(dir(L))

์ถœ๋ ฅ ๊ฒฐ๊ณผ

['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

(๋”๋ธ”์–ธ๋”๋ฐ”)iter(๋”๋ธ”์–ธ๋”๋ฐ”) ํ•จ์ˆ˜๊ฐ€ ์กด์žฌํ•˜๋Š” ๊ฒƒ์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค.
= ๋ฐ˜๋ณต์ด ๊ฐ€๋Šฅํ•œ ๊ฐ์ฒด๋ผ๋Š” ๋œป

๊ทธ ๋‹ค์Œ์€ (๋”๋ธ”์–ธ๋”๋ฐ”)next(๋”๋ธ”์–ธ๋”๋ฐ”)๋ฉ”์†Œ๋“œ์˜ ๋™์ž‘์„ ํ™•์ธํ•ด ๋ณด๋ ค๊ณ  ํ•œ๋‹ค.
์ผ๋‹จ L์˜ list์•ˆ์— (๋”๋ธ”์–ธ๋”๋ฐ”)next(๋”๋ธ”์–ธ๋”๋ฐ”)๋ผ๋Š” ๋ฉ”์†Œ๋“œ๊ฐ€ ์—†์„ ๊ฒƒ์ด๋‹ค. ์™œ๋ƒํ•˜๋ฉด L ์ด๋ผ๋Š” list๋Š” Iterable์ด์ง€ Iterator๊ฐ€ ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

์•ž์„œ ๋งํ–ˆ๋“ฏ, Iterable์„ Iterator๋กœ ๋ฐ”๊ฟ”์ฃผ๋ ค๋ฉด (๋”๋ธ”์–ธ๋”๋ฐ”)iter(๋”๋ธ”์–ธ๋”๋ฐ”)๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

iterator_L ์ด๋ผ๋Š” ๋ณ€์ˆ˜์— L์ด๋ผ๋Š” ๋ฆฌ์ŠคํŠธ์— (๋”๋ธ”์–ธ๋”๋ฐ”)iter(๋”๋ธ”์–ธ๋”๋ฐ”)๋ผ๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ด์ฃผ๋ฉด, iterator_L์ด๋ผ๋Š” ๋ณ€์ˆ˜๋Š” next() ๋ฉ”์†Œ๋“œ๊ฐ€ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•œ Iterator๊ฐ€ ๋œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  (๋”๋ธ”์–ธ๋”๋ฐ”)next(๋”๋ธ”์–ธ๋”๋ฐ”)๋ฉ”์†Œ๋“œ์˜ ๊ธฐ๋Šฅ์„ ๋ณด๊ธฐ์œ„ํ•ด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ–ˆ๋‹ค.

L = [1, 2, 3]
iterator_L = L.__iter__()

print(iterator_L.__next__())
print(iterator_L.__next__())
print(iterator_L.__next__())
print(iterator_L.__next__())

์ถœ๋ ฅ ๊ฒฐ๊ณผ

1
2
3
Traceback (most recent call last):
  File "/Users/minholee/Desktop/test_python/test3.py", line 7, in <module>
    print(iterator_L.__next__())
StopIteration

๋งˆ์ง€๋ง‰ ๋ถ€๋ถ„์— StopIteration์ด ๋ฐœ์ƒํ•˜๋Š” ๊ฒƒ์„ ํ™•์ธ ๊ฐ€๋Šฅํ•˜๋‹ค.
list์˜ index์—์„œ ๋ฒ—์–ด๋‚˜ ๊ฐ’์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.
์œ„์™€ ๊ฐ™์ด ๋ฒ—์–ด๋‚˜์„œ ๊ฐ€์ ธ์˜ค๋ ค๊ณ  ํ•˜๋ฉด StopIteration์ด๋ผ๋Š” ๋ฌธ๊ตฌ๊ฐ€ ๋œฌ๋‹ค.

(๋”๋ธ”์–ธ๋”๋ฐ”)iter(๋”๋ธ”์–ธ๋”๋ฐ”)๋ฉ”์†Œ๋“œ์™€ (๋”๋ธ”์–ธ๋”๋ฐ”)next(๋”๋ธ”์–ธ๋”๋ฐ”)๋ฉ”์†Œ๋“œ๋ฅผ ํŽธํ•˜๊ฒŒ ์‚ฌ์šฉํ•ด์ฃผ๋Š” iter์™€ next๊ฐ€ ์žˆ๋‹ค.

iter(๊ฐ์ฒด) ๋Š” ๊ฐ์ฒด์˜ (๋”๋ธ”์–ธ๋”๋ฐ”)iter(๋”๋ธ”์–ธ๋”๋ฐ”)๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ด์ฃผ๊ณ ,
next(๊ฐ์ฒด) ๋Š” ๊ฐ์ฒด์˜ (๋”๋ธ”์–ธ๋”๋ฐ”)next(๋”๋ธ”์–ธ๋”๋ฐ”)๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ด์ค€๋‹ค.

L์ด๋ผ๋Š” ๋ฆฌ์ŠคํŠธ๊ฐ€ ์žˆ์„ ๋•Œ,

this_is_iterator = L.__iter__()     # 1
this_is_iterator = iter(L)          # 2

X = this_is_iterator.__next__()     # 3
X = next(this_is_iterator)          # 4 

1์ด๋ž‘ 2๊ฐ€ ๊ฐ™์€ ๋ง์ด๊ณ , 3์ด๋ž‘ 4๊ฐ€ ๊ฐ™์€ ๋ง์ด๋ผ๋Š” ๋œป์ด๋‹ค.

L = [1, 2, 3]

for x in L:
    print(x ** 2)

์œ„์˜ ์ฝ”๋“œ๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์ด ์“ธ ์ˆ˜ ์žˆ๋‹ค.

L = [1, 2, 3]
I = iter(L)
while True:
    try:
        X = next(I)
    except StopIteration:
        break
    print(X ** 2)

โ—๏ธโ—๏ธโ—๏ธ์šฐ๋ฆฌ๊ฐ€ ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” for๋ฌธ์€ ๋“ค์–ด์˜ค๋Š” iterable์„ loopingํ•˜๋Š” ๋™์•ˆ, python ๋‚ด๋ถ€์—์„œ ์ž„์‹œ๋กœ ๊ทธ Iterable์„ Iterator๋กœ ์ž๋™์œผ๋กœ ๋ณ€ํ™˜ํ•ด์ค€๋‹ค๊ณ  ํ•œ๋‹ค.

์ถœ์ฒ˜

1๏ธโƒฃ ๋ฌธ์ œ

D = {'a': 1, 'b': 2, 'c': 3}
for key in D:
    print(key)

๋”•์…”๋„ˆ๋ฆฌ๋„ ๋ฐ˜๋ณต๊ฐ€๋Šฅํ•œ ๊ฐ์ฒด๋ผ์„œ ์•ž์„œ๋ณธ ๋ฆฌ์ŠคํŠธ์™€ ๊ฐ™์ด iterํ•จ์ˆ˜์™€ nextํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ณ  ํŒŒ์ด์ฌ ๊ธฐ๋ณธํ•จ์ˆ˜์ธ iter, next ๋˜ํ•œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹ค์Œ์˜ ๊ฐ„๋‹จํ•œ ํ‚ค๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๋”•์…”๋„ˆ๋ฆฌ์— ๋Œ€ํ•œ for ๋ฌธ์„ while๋ฌธ์œผ๋กœ ๊ตฌํ˜„ํ•ด ๋ณด์ž.

D = {'a': 1, 'b': 2, 'c': 3}
changed_iterator = iter(D)
while True:
    try:
        element = next(changed_iterator)
    except StopIteration:
        break
    print(element)

๋”•์…”๋„ˆ๋ฆฌ๋Š” ์ž๋™์œผ๋กœ key๊ฐ’๋งŒ ์ถœ๋ ฅ์ด ๋œ๋‹ค.
๋งŒ์•ฝ ์—ฌ๊ธฐ์„œ key๊ฐ’๊ณ  value ๊ฐ’์„ ๊ฐ™์ด ์ฃผ๊ณ ์‹ถ๋‹ค๋ฉด,

์ถœ๋ ฅ ๋ถ€๋ถ„๋งŒ

print(element, D[element])

๋กœ ๋ฐ”๊ฟ”์ฃผ๋ฉด ๋œ๋‹ค.

profile
๐Ÿ‡ Rabbit can take a rest, but ๐Ÿข turtle can't

0๊ฐœ์˜ ๋Œ“๊ธ€