π μ΄ ν¬μ€ν μμλ Pythonμ Lambda expressionμ λν΄ μ 리νμμ΅λλ€.
π₯ lambda ννμμ΄λ?
π₯ lambda ννμμ νμ©
π₯ Assignment
βοΈ lambdaλ ν¨μμ μΆμ½ν ννμμΌλ‘ μ΅λͺ
ν¨μ λλ λλ€ ν¨μλΌ λΆλ¦
λλ€.
βοΈ λλ€ ννμκ³Ό μΌλ° ν¨μμ μ°¨μ΄μ μ μ΄λ¦μ μ 무μ΄λ©°, returnμ΄ μμ΄λ lambda ν¨μλ κ²°κ³Όλ₯Ό λ°νν©λλ€.
βοΈ lambda ν¨μλ λ€λ₯Έ κ³³μμ μ¬μ¬μ©λμ§ μλ κ°λ¨ν bodyλ₯Ό κ°μΆ ν¨μκ° νμν λ κ°νΈνκ² μ¬μ©λ©λλ€.
print((lambda x,y : x // 2)(100,25)) # 50
βοΈ μλ κ³μ°κΈ° κΈ°λ₯μ ꡬνν ν¨μλ₯Ό λλ€ννμμΌλ‘ μμ νλ©΄ λ κ°νΈνκ² ννλ₯Ό κ°μ§ κ²μ λ³Ό μ μμ΅λλ€.
# μΌλ°ν¨μ def add(x,y): return x+y def sub(x,y): return x-y def div(x,y): return x/y def mul(x,y): return x*y calculator = [add, sub, div, mul] print(calculator[0](10,5)) # 15 print(calculator[1](10,5)) # 5 print(calculator[2](10,5)) # 2.0 print(calculator[3](10,5)) # 50
βοΈ μλμ²λΌ κ°λ¨ν λ‘μ§μ lambdaλ‘ ννν λ, λμ± μ§κ΄μ μ΄κΈ° λλ¬Έμ κ°λ μ±μ΄ μ’μ΅λλ€.
# lambda ννμ calculator = [ lambda x,y : x+y, lambda x,y : x-y, lambda x,y : x/y, lambda x,y : x*y, ] print(calculator[0](10,5)) # 15 print(calculator[1](10,5)) # 5 print(calculator[2](10,5)) # 2.0 print(calculator[3](10,5)) # 50
βοΈ lambda ννμ λ΄λΆμ λ³μλ₯Ό μ μΈνμ¬ μ¬μ©νλ©΄ SyntaxErrorκ° λ°μν©λλ€.
lambda_func = (lambda x: y=3; x//y) print(lambda_func(27)) # SyntaxError: invalid syntax
βοΈ μ΄μ μ΄λ€ λ³μλ₯Ό lambda ννμ λ΄λΆλ‘ μ λ¬νκ³ μΆλ€λ©΄ parameterλ‘ μ λ¬νλμ§ μΈλΆμ μ μΈν΄μ£Όμ΄ ν©λλ€.
# λ³μ μΈλΆμ μ μΈ y = 3 lambda_func = (lambda x: x//y) print(lambda_func(27)) # 9
βοΈ iterableν μλ£λ₯Ό λ°μ μμλ₯Ό μ κ³±νλ ν¨μλ μλμ κ°μ΄ ꡬνν μ μμ΅λλ€.
def func(x): return x**2 res = list(map(func, [1,2,3,4,5])) print(res) # [1, 4, 9, 16, 25]
βοΈ filter, map λ± μ½λ°±ν¨μλ₯Ό λ°μ μ²λ¦¬ν λ, lambda ννμμΌλ‘ ꡬννλ©΄ κ°νΈν©λλ€.
res = list(map(lambda x: x**2, [1,2,3,4,5])) print(res) # [1, 4, 9, 16, 25]
βοΈ lambda ν¨μλ filter, map, zip λ±κ³Ό ν¨κ» μ¬μ©ν λ κΆν©μ΄ μ’μ΅λλ€.
numbers = [1,2,3,4,5] multiplier = [1,3,2,4,2] squared = list(map(lambda x: x[0] ** x[1], zip(numbers, multiplier))) print(squared) # [1, 8, 9, 256, 25]
βοΈ lambda ννμμ μ€μ²©λ νμ©ν©λλ€. λ¨, λ무 κΉμ μ€μ²©μ κ°λ μ±μ΄ λ¨μ΄μ§λλ€.
numbers = [1,2,3,4,5] multiplier = [1,3,2,4,2] squared = list(filter(lambda x: x > 10, list(map(lambda x: x[0] ** x[1], zip(numbers, multiplier))))) print(squared) # [256, 25]
βοΈ λ€μμ λΉλ°λ²νΈμ κΈΈμ΄μ λλ¬Έμκ° ν¬ν¨λ κ²μ νμΈνλ κ°λ¨ν ν¨μμ λλ€.
def check_password(password): if len(password) < 8: return 'SHORT_PASSWORD' if not any(c.isupper() for c in password): return 'NO_CAPITAL_LETTER_PASSWORD' return True print(check_password('123')) # SHORT_PASSWORD print(check_password('12356789f')) # NO_CAPITAL_LETTER_PASSWORD print(check_password('123456789F')) # True
βοΈ μμ ν¨μλ₯Ό λλ€ννμμΌλ‘ μμ νμ¬ κ΅¬ννλ©΄ μλμ κ°μ΅λλ€.
βοΈ λλ€ννμμμ if~elseλ¬Έλ μΆμ½([ifμΌ λ body] if 쑰건 else [elseμΌ λ body]
) κ°λ₯ν©λλ€.
lambdas = [ lambda x: 'SHORT_PASSWORD' if len(x) < 8 else None, lambda x: 'NO_CAPITAL_LETTER_PASSWORD' if not any(i.isupper() for i in x) else None ] def check_password_using_lambda(password): for f in lambdas: if f(password) is not None: return f(password) return True print(check_password_using_lambda('123')) # SHORT_PASSWORD print(check_password_using_lambda('12356789f')) # NO_CAPITAL_LETTER_PASSWORD print(check_password_using_lambda('123456789F')) # True