파이썬 람다 (익명 함수) | 필터,지도, 감소

람다 인수 : 표현
# Python code to illustrate cube of a number 
# showing difference between def() and lambda(). 
def cube(y): 
	return y*y*y; 

g = lambda x: x*x*x 
print(g(7)) 

print(cube(5)) 

필터 람다의 사용 () ()

# Python code to illustrate 
# filter() with lambda() 
li = [5, 7, 22, 97, 54, 62, 77, 23, 73, 61] 
final_list = list(filter(lambda x: (x%2 != 0) , li)) 
print(final_list) 

지도 람다의 사용 () ()

# Python code to illustrate 
# map() with lambda() 
# to get double of a list. 
li = [5, 7, 22, 97, 54, 62, 77, 23, 73, 61] 
final_list = list(map(lambda x: x*2 , li)) 
print(final_list) 

감소와 람다 ()의 사용 ()

# Python code to illustrate 
# reduce() with lambda() 
# to get sum of a list 
from functools import reduce
li = [5, 8, 10, 20, 50, 100] 
sum = reduce((lambda x, y: x + y), li) 
print (sum) 

 

게시 된 128 개 원래 기사 · 원 찬양 90 · 전망 4856

추천

출처blog.csdn.net/weixin_45405128/article/details/103993593