[PYTHON] map,filter

엄마제똥먹어·2022년 10월 4일
0

파이썬에는 map과 filter 라는 함수가 있다


power = lambda x: x * x # 함수
under_3 = lambda x: x < 3 # filter조건 = 3보다 작을떄

list_input_a = [1,2,3,4,5,6] # input값은 고정
output_a = map(power,list_input_a) # 함수(함수 -> 매개변수)

print("#map() 실행결과") # map의 실행결과
print("map(power,list_input_a) :" , output_a) # no llist
print( "map(power,list_input_a):" , list(output_a)) # yes list


output_b = filter(under_3 , list_input_a) # 출력은 하지만 filter를 조건으로 둠

print("#filter() 의 실행결과") # filter을 거친 결과
print("filter(under_3,list_input_A) : " , list_input_a) # input값 고정
print("filter(under_3,list_input_a : " , output_b) # no list
print("filter(under_3,list_input_b) : " , list(output_b)) # yes list

map은 함수를 매개변수화 해서 함수로 넘기는 함수이다
list_input_a 의 저장되어 있는 값을 power을 통해 output_a 로 넘긴다

filter는 list_input_a 의 값을 under_3 를 통해 output_b 로 넘긴다

-출력결과
->
#map() 실행결과
map(power,list_input_a) : <map object at 0x1028e9fa0>
map(power,list_input_a) : [1, 4, 9, 16, 25, 36]

#filter() 의 실행결과
filter(under_3,list_input_a) : [1, 2, 3, 4, 5, 6]
filter(under_3,list_input_a) : <filter object at 0x1028e9e50>
filter(under_3,list_input_b) : [1, 2]

0개의 댓글