"익명 함수"의 두 판 사이의 차이

22번째 줄: 22번째 줄:
<source lang='Python'>
<source lang='Python'>
numbers = [1, 2, 3, 4, 5, 6]
numbers = [1, 2, 3, 4, 5, 6]
print( filter(lambda x: x%2==0, numbers) )
print( list(filter(lambda x: x % 2 == 0, numbers)) )
# [2, 4, 6]
# [2, 4, 6]
print( list(map(lambda x: x * 2 - 1, numbers)) )
# [1, 3, 5, 7, 9, 11]
</source>
</source>



2014년 8월 21일 (목) 13:01 판

1 개요

anonymous function, function constant, function literal, lambda function
익명 함수, 람다 함수
  • 대리자나 식 트리 형식을 만드는 데 사용할 수 있음

2 Python 예시

print( (lambda x: x*x)(5) )
# 25
print( lambda x: x*x )
# <function <lambda> at 0x02D30390>
def square(x): return x*x
print( square )
# <function square at 0x02D678A0>
filter, map, reduce
numbers = [1, 2, 3, 4, 5, 6]
print( list(filter(lambda x: x % 2 == 0, numbers)) )
# [2, 4, 6]
print( list(map(lambda x: x * 2 - 1, numbers)) )
# [1, 3, 5, 7, 9, 11]

3 같이 보기

4 참고 자료

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}