Guidelines

μ΄ν„°λŸ¬λΈ”(iterable)κ³Ό μ΄ν„°λ ˆμ΄ν„°(iterator)의 차이점

νŒŒμ΄μ¬μ—μ„œ μ΄ν„°λŸ¬λΈ”(iterable)은 리슀트, νŠœν”Œ, λ¬Έμžμ—΄κ³Ό 같이 μš”μ†Œ(Element)λ₯Ό ν•˜λ‚˜μ”© λ°˜ν™˜ν•  수 μžˆλŠ” 반볡 κ°€λŠ₯ν•œ 객체λ₯Ό μ˜λ―Έν•©λ‹ˆλ‹€.

μ΄ν„°λŸ¬λΈ”μ€ for 반볡문, list(), set(), tuple()κ³Ό 같은 ν•¨μˆ˜μ—μ„œ μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

μ΄ν„°λŸ¬λΈ” μ˜ˆμ‹œ
# λ¦¬μŠ€νŠΈλŠ” μ΄ν„°λŸ¬λΈ” my_list = [1, 2, 3] # λ¦¬μŠ€νŠΈλŠ” 반볡 κ°€λŠ₯ν•œ 객체 for item in my_list: print(item)

반면, μ΄ν„°λ ˆμ΄ν„°(iterator)λŠ” μ΄ν„°λŸ¬λΈ”μ˜ μš”μ†Œλ“€μ„ 순차적으둜 μ ‘κ·Όν•  수 μžˆλŠ” 객체λ₯Ό λœ»ν•©λ‹ˆλ‹€.

μ΄ν„°λ ˆμ΄ν„°λŠ” iter() ν•¨μˆ˜λ‘œ μ΄ν„°λŸ¬λΈ”λ‘œλΆ€ν„° 생성할 수 있으며, next() ν•¨μˆ˜λ₯Ό μ‚¬μš©ν•΄ λ‹€μŒ μš”μ†Œμ— μ ‘κ·Όν•  수 μžˆμŠ΅λ‹ˆλ‹€.

μ΄ν„°λ ˆμ΄ν„° μ˜ˆμ‹œ
# λ¦¬μŠ€νŠΈλŠ” μ΄ν„°λŸ¬λΈ” my_list = [1, 2, 3] # μ΄ν„°λ ˆμ΄ν„° 생성 my_iterator = iter(my_list) # λ‹€μŒ μš”μ†Œμ— μ ‘κ·Ό print(next(my_iterator)) # 1 print(next(my_iterator)) # 2 print(next(my_iterator)) # 3
Mission
0 / 1

μ΄ν„°λŸ¬λΈ”(iterable)κ³Ό μ΄ν„°λ ˆμ΄ν„°(iterator)의 차이점으둜 μ˜¬λ°”λ₯Έ 것은 λ¬΄μ—‡μΈκ°€μš”?

μ΄ν„°λŸ¬λΈ”μ€ μˆœνšŒν•  수 μ—†κ³ , μ΄ν„°λ ˆμ΄ν„°λ§Œ μˆœνšŒν•  수 μžˆλ‹€.

μ΄ν„°λŸ¬λΈ”μ€ μš”μ†Œλ₯Ό ν•˜λ‚˜μ”© λ°˜ν™˜ν•  수 있고, μ΄ν„°λ ˆμ΄ν„°λŠ” μ΄ν„°λŸ¬λΈ”μ—μ„œ μƒμ„±λœλ‹€.

μ΄ν„°λŸ¬λΈ”μ€ next() ν•¨μˆ˜λ‘œ μš”μ†Œμ— μ ‘κ·Όν•˜κ³ , μ΄ν„°λ ˆμ΄ν„°λŠ” for 반볡문으둜 μˆœνšŒν•œλ‹€.

μ΄ν„°λŸ¬λΈ”κ³Ό μ΄ν„°λ ˆμ΄ν„°λŠ” λ™μΌν•œ κ°œλ…μ΄λ‹€.

Guidelines

AI Tutor

Publish

Design

Upload

Notes

Favorites

Help

Code Editor

Run
Generate

Execution Result