Guidelines

filter(), map() ν•¨μˆ˜λ‘œ μ‹œν€€μŠ€ μ²˜λ¦¬ν•˜κΈ°

filter() ν•¨μˆ˜μ™€ map() ν•¨μˆ˜λŠ” μ‹œν€€μŠ€(리슀트, νŠœν”Œ λ“± 반볡 κ°€λŠ₯ν•œ 객체)의 μš”μ†Œλ“€μ„ μ²˜λ¦¬ν•˜λŠ” 데 μ‚¬μš©λ©λ‹ˆλ‹€.

filter() ν•¨μˆ˜λŠ” 쑰건에 λ§žλŠ” μš”μ†Œλ“€λ§Œ ν•„ν„°λ§ν•˜λŠ” 데 μ‚¬μš©λ˜λ©°, map() ν•¨μˆ˜λŠ” 각 μš”μ†Œμ— ν•¨μˆ˜λ₯Ό μ μš©ν•˜μ—¬ μƒˆλ‘œμš΄ μ‹œν€€μŠ€λ₯Ό μƒμ„±ν•©λ‹ˆλ‹€.


filter() ν•¨μˆ˜μ˜ μ‚¬μš©

filter() ν•¨μˆ˜λŠ” 주어진 ν•¨μˆ˜κ°€ Trueλ₯Ό λ°˜ν™˜ν•˜λŠ” λͺ¨λ“  μš”μ†Œλ‘œ κ΅¬μ„±λœ μƒˆ μ‹œν€€μŠ€λ₯Ό λ§Œλ“­λ‹ˆλ‹€.

filter ν•¨μˆ˜μ˜ 첫번째 μΈμžλŠ” 콜백 ν•¨μˆ˜μ΄λ©°, λ‘λ²ˆμ§Έ μΈμžλŠ” μ‹œν€€μŠ€μž…λ‹ˆλ‹€.

filter() ν•¨μˆ˜ ꡬ성
filter(function, iterable)

주둜 쑰건에 따라 μš”μ†Œλ₯Ό 필터링할 λ•Œ μ‚¬μš©λ©λ‹ˆλ‹€.

filter() ν•¨μˆ˜μ˜ μ‚¬μš©
# 짝수λ₯Ό ν•„ν„°λ§ν•˜λŠ” 콜백 ν•¨μˆ˜ def is_even(number): return number % 2 == 0 # 숫자 리슀트 numbers = [1, 2, 3, 4, 5, 6] # filter() ν•¨μˆ˜ μ‚¬μš© even_numbers = filter(is_even, numbers) print(list(even_numbers)) # [2, 4, 6]

map() ν•¨μˆ˜μ˜ μ‚¬μš©

map() ν•¨μˆ˜λŠ” 주어진 ν•¨μˆ˜λ₯Ό μ‹œν€€μŠ€μ˜ 각 μš”μ†Œμ— μ μš©ν•˜μ—¬ μƒˆλ‘œμš΄ μ‹œν€€μŠ€λ₯Ό μƒμ„±ν•©λ‹ˆλ‹€. 이 ν•¨μˆ˜λŠ” 데이터 λ³€ν™˜ μž‘μ—…μ— 자주 μ‚¬μš©λ©λ‹ˆλ‹€.

map() ν•¨μˆ˜μ˜ μ‚¬μš©
# 숫자λ₯Ό μ œκ³±ν•˜λŠ” 콜백 ν•¨μˆ˜ numbers = [1, 2, 3, 4, 5, 6] def square(number): return number * number # map() ν•¨μˆ˜ μ‚¬μš© squared_numbers = map(square, numbers) print(list(squared_numbers)) # [1, 4, 9, 16, 25, 36]

Guidelines

AI Tutor

Publish

Design

Upload

Notes

Favorites

Help

Code Editor

Run
Generate

Execution Result