TIL31. Python : Lambda expression

ID짱재·2021λ…„ 10μ›” 11일
0

Python

λͺ©λ‘ 보기
37/39
post-thumbnail

πŸ“Œ 이 ν¬μŠ€νŒ…μ—μ„œλŠ” Python의 Lambda expression에 λŒ€ν•΄ μ •λ¦¬ν•˜μ˜€μŠ΅λ‹ˆλ‹€.



🌈 Lambda expression

πŸ”₯ lambda ν‘œν˜„μ‹μ΄λž€?

πŸ”₯ lambda ν‘œν˜„μ‹μ˜ ν™œμš©

πŸ”₯ Assignment



1. lambda ν‘œν˜„μ‹μ΄λž€?

πŸ€” lambda ν‘œν˜„μ‹μ€ ν•¨μˆ˜μ˜ μΆ•μ•½ν˜• ν‘œν˜„μ‹μž…λ‹ˆλ‹€.

βœ”οΈ lambdaλŠ” ν•¨μˆ˜μ˜ μΆ•μ•½ν˜• ν‘œν˜„μ‹μœΌλ‘œ 읡λͺ… ν•¨μˆ˜ λ˜λŠ” λžŒλ‹€ ν•¨μˆ˜λΌ λΆ€λ¦…λ‹ˆλ‹€.

βœ”οΈ λžŒλ‹€ ν‘œν˜„μ‹κ³Ό 일반 ν•¨μˆ˜μ˜ 차이점은 μ΄λ¦„μ˜ 유무이며, return이 없어도 lambda ν•¨μˆ˜λŠ” κ²°κ³Όλ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.
βœ”οΈ lambda ν•¨μˆ˜λŠ” λ‹€λ₯Έ κ³³μ—μ„œ μž¬μ‚¬μš©λ˜μ§€ μ•ŠλŠ” κ°„λ‹¨ν•œ bodyλ₯Ό κ°–μΆ˜ ν•¨μˆ˜κ°€ ν•„μš”ν•  λ•Œ κ°„νŽΈν•˜κ²Œ μ‚¬μš©λ©λ‹ˆλ‹€.

print((lambda x,y : x // 2)(100,25)) # 50

πŸ€” lambda ν‘œν˜„μ‹κ³Ό 일반 ν•¨μˆ˜μ˜ 비ꡐ

βœ”οΈ μ•„λž˜ 계산기 κΈ°λŠ₯을 κ΅¬ν˜„ν•œ ν•¨μˆ˜λ₯Ό λžŒλ‹€ν‘œν˜„μ‹μœΌλ‘œ μˆ˜μ •ν•˜λ©΄ 더 κ°„νŽΈν•˜κ²Œ ν˜•νƒœλ₯Ό 가진 것을 λ³Ό 수 μžˆμŠ΅λ‹ˆλ‹€.

# μΌλ°˜ν•¨μˆ˜
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 ν‘œν˜„μ‹μ€ ν‘œν˜„μ‹ 내뢀에 λ³€μˆ˜λ₯Ό μ„ μ–Έν•  수 μ—†μŠ΅λ‹ˆλ‹€.

βœ”οΈ 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


2. lambda ν‘œν˜„μ‹μ˜ ν™œμš©

πŸ€” callback ν•¨μˆ˜μ™€ lambda ν‘œν˜„μ‹

βœ”οΈ 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 ν‘œν˜„μ‹μ˜ 쀑첩

βœ”οΈ 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]


3. Assignment

βœ”οΈ λ‹€μŒμ€ λΉ„λ°€λ²ˆν˜Έμ˜ 길이와 λŒ€λ¬Έμžκ°€ ν¬ν•¨λœ 것을 ν™•μΈν•˜λŠ” κ°„λ‹¨ν•œ ν•¨μˆ˜μž…λ‹ˆλ‹€.

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
profile
Keep Going, Keep Coding!

0개의 λŒ“κΈ€