Development/Python
파이썬 문법 : map, filter 함수
폴피드
2023. 2. 28. 07:44
- map 함수
- map(함수, 순서가 있는 자료형)
- map 의 결과는 map object 이기 때문에 사용하기 편한 list 형태로 변환한다.
def remove_blank(x):
return x.strip()
items=[' mouse', ' monitor ']
items=list(map(remove_blank, items))
print(items)
# output
# ['mouse', 'monitor']
# 람다함수로 표기할 경우
items=list(map(lambda x:x.strip(), items))
- filter 함수
- filter(함수, 순서가 있는 자료형)
def func(x):
return x < 0
print(list(filter(func, [-3, 0, 2, 7, -7])))
# output
# [-3, -7]
# 람다함수로 표현
print(list(filter(lambda x:x<0, [-3, 0, 2, 7, -7])))
728x90
반응형