Guidelines

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

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

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


μ΄ν„°λŸ¬λΈ”(Iterable)

μ΄ν„°λŸ¬λΈ”μ€ for 반볡문, list(), set(), tuple() λ“±μœΌλ‘œ μˆœνšŒν•  수 μžˆλŠ” 데이터 νƒ€μž…μž…λ‹ˆλ‹€.

λ¬Έμžμ—΄, 리슀트, νŠœν”Œ, λ”•μ…”λ„ˆλ¦¬ 등이 이에 ν•΄λ‹Ήν•©λ‹ˆλ‹€.

μ΄ν„°λŸ¬λΈ” μ˜ˆμ‹œ
numbers = [1, 2, 3] # 리슀트 for number in numbers: # for 반볡문 print(number) # 좜λ ₯ κ²°κ³Ό: 1, 2, 3

μ΄ν„°λ ˆμ΄ν„°(Iterator)

μ΄ν„°λ ˆμ΄ν„°λŠ” μ΄ν„°λŸ¬λΈ”(Iterable)λ‘œλΆ€ν„° iter() ν•¨μˆ˜λ₯Ό μ‚¬μš©ν•΄ μƒμ„±ν•©λ‹ˆλ‹€. μ΄ν„°λ ˆμ΄ν„°λŠ” next() ν•¨μˆ˜λ‘œ λ‹€μŒ μš”μ†Œμ— μ ‘κ·Όν•©λ‹ˆλ‹€.

μ΄ν„°λ ˆμ΄ν„° μ˜ˆμ‹œ
numbers = [1, 2, 3] # 리슀트 iterator = iter(numbers) # μ΄ν„°λ ˆμ΄ν„° 생성 print(next(iterator)) # 1 print(next(iterator)) # 2 print(next(iterator)) # 3

μ΄ν„°λ ˆμ΄ν„°μ™€ μ΄ν„°λŸ¬λΈ” ν™œμš©

  • μ΄ν„°λŸ¬λΈ”: 데이터 μ»¬λ ‰μ…˜μ„ μˆœνšŒν•˜κ³ , 각 μš”μ†Œμ— λŒ€ν•œ μž‘μ—…μ„ μˆ˜ν–‰ν•  λ•Œ μ‚¬μš©ν•©λ‹ˆλ‹€.

  • μ΄ν„°λ ˆμ΄ν„°: μ’€ 더 μ„Έλ°€ν•œ μ œμ–΄κ°€ ν•„μš”ν•œ κ²½μš°λ‚˜ λ¬΄ν•œ μ‹œν€€μŠ€λ₯Ό λ‹€λ£° λ•Œ μœ μš©ν•©λ‹ˆλ‹€.

Guidelines

AI Tutor

Publish

Design

Upload

Notes

Favorites

Help

Code Editor

Run
Generate

Execution Result