프로그래밍/Python

[Python] map, filter 함수

잡뿌 2021. 11. 28. 17:51
반응형
#1. map 함수
# - 사용이유
# - 기존 리스트를 수정해서 새로운 리스트를 만들때 사용


# - 사용방법
# map(함수, 순서가 있는 자료형)

print(list(map(int, ['1', '2', '3'])))

items = ['   logitecmouse', 'epsol keyboard  ']

# def strip_all(x):
#     return x.strip()
# items = list(map(strip_all, items))

items = list(map(lambda x: x.strip(), items))

print(items)

#filter 함수
# - 사용이유
# - 기존 리스트에 조건을 만족하는 요소만 추출

# - 사용방법
# filter(함수, 순서가 있는 자료형)

def func(x):
    return x<0
print(list(filter(func, [-5, -3, 0, 1, 2, 3])))
반응형