본문 바로가기
python

python2의 map, filter, reduce 함수

by hs_seo 2015. 12. 28.

[python2의 map, filter, reduce 함수]


map 함수를 이용하여 리스트의 값들에 동일한 로직을 적용하여 데이터를 변환하고,

filter 함수를 이용하여 리스트의 값들을 필터링,

reduce 함수를 이용하여 리스트의 값을 하나로 모은다.


temp_list = range(0, 10)

# python map, filter, reduce 함수
# map 함수는 iterable 에 동일한 함수를 적용한다. 
# map(function, iterable, ...)
print map(lambda x: x + 1, temp_list)

# filter 함수는 iterable 에서 조건에 맞는 함수를 제거한다. 
print filter(lambda x: x > 3, temp_list)

# reduce 함수는 iterable 의 모든 아이템을 이용하여 결과를 생성한다. 
# ex) 리스트에서 가장 큰 값, 리스트 안의 값들의 합
print reduce(lambda x, y: x + y, temp_list)
print reduce(lambda x, y: x if x > y else y, temp_list)


반응형

'python' 카테고리의 다른 글

SyntaxError: Non-ASCII character 해결하기  (0) 2016.01.20
pip의 패키지 업그레이드 하기  (0) 2016.01.20
[Python] ==과 is 의 차이  (0) 2015.08.05
[Python] http 리퀘스트 처리하기  (2) 2015.06.23
[python] 문자열 치환 방법  (0) 2015.06.12