[코딩도장] 32.lambda

Mori·2022년 10월 15일
0

코딩도장

목록 보기
1/1
post-thumbnail

32.1 lambda 기본 사용법

lambda (매개변수) : (식)

def plus_ten(x):
	return x+10

lambda x : x+10

32.2 map과 같이 활용

map(lambda x: (매개변수)x (식1)str(x) if (조건식1)x%3==0 else (식2)x,a))

#map
print(list(map(lambda x:x+10, [1,2,3])))

print(list(map(lambda x: str(x) if x%3==0 else x,a))

32.2 filter와 같이 활용

a = [1,4,8,7,12,14,17,27]

print(list(filter(lambda x:x%10 ==7 , a)))

32.4 연습문제 : 이미지 파일만 가져오기

files = ['font', '1.png', '10.jpg', '11.gif', '2.jpg', '3.png', 'table.xslx', 'spec.docx']
print(list(filter(lambda x: x.find('.png')==True or x.find('.jpg')==True , files )))
==========================

['1.png', '10.jpg', '2.jpg', '3.png']
profile
선한 영향력을 추구하는 데이터분석가 Mori입니다 :)

0개의 댓글