리μ€νΈλ₯Ό λ³΄λ€ λ κ°λ¨νκ² λ§λλ 리μ€νΈ λ΄ν¬
리μ€νΈ λ΄ν¬(List Comprehensions)
λ for λ°λ³΅λ¬Έ
과 쑰건문
μ μ¬μ©νμ¬ ν μ€μ μ½λλ‘ λ¦¬μ€νΈλ₯Ό λ§λλλ€.
리μ€νΈ λ΄ν¬ ꡬ쑰
[ννμ for νλͺ© in λ°λ³΅ κ°λ₯ν κ°μ²΄]
리μ€νΈ λ΄ν¬ νμ©
리μ€νΈ λ΄ν¬λ λκ΄νΈ([]
) μμ ννμκ³Ό for λ°λ³΅λ¬Έ, κ·Έλ¦¬κ³ νμμ λ°λΌ if 쑰건문μ ν¬ν¨νμ¬ μμ±ν©λλ€.
μ΄λ₯Ό ν΅ν΄ κΈ°μ‘΄μ 리μ€νΈλ νν κ°μ λ°λ³΅ κ°λ₯ν κ°μ²΄λ₯Ό κΈ°λ°μΌλ‘ μλ‘μ΄ λ¦¬μ€νΈλ₯Ό μμ±ν μ μμ΅λλ€.
리μ€νΈ λ΄ν¬ μμ
numbers = [1, 2, 3, 4, 5] squared = [num ** 2 for num in numbers] print("squared:", squared) # [1, 4, 9, 16, 25]
리μ€νΈ λ΄ν¬λ κΈ°μ‘΄ 리μ€νΈμ κ° μμμ μ°μ°μ μ μ©ν΄ μλ‘μ΄ λ¦¬μ€νΈλ₯Ό μμ±νκ±°λ, νΉμ 쑰건μ λ§μ‘±νλ μμλ§ ν¬ν¨νλ μλ‘μ΄ λ¦¬μ€νΈλ₯Ό λ§λ€ λ μ¬μ©λ©λλ€.
쑰건μ ν¬ν¨ν 리μ€νΈ λ΄ν¬
numbers = [1, 2, 3, 4, 5] # μ§μλ§ ν¬ν¨ even_numbers = [num for num in numbers if num % 2 == 0] print(even_numbers) # [2, 4]
Mission
0 / 1
리μ€νΈ λ΄ν¬μμ μ¬μ©λλ λ°λ³΅λ¬Έμ 무μμΈκ°μ?
if
do
while
for
Guidelines
AI Tutor
Publish
Design
Upload
Notes
Favorites
Help
Code Editor
Run
Generate
Execution Result