카테고리 없음2022. 4. 15. 10:17

map, reduce, filter..

 

 

score = lambda x: "A등급" if x >= 80 else ("B등급" if x >= 70 else "C등급")

 

# ----------------------------------------------------------

add = lambda a, b: a+b
result = add(3, 5) 

 

# ----------------------------------------------------------

arr = [[1,2,9],
       [5,1,8],
       [3,7,9]]
# 2열이 가장 큰 행 
print(max(arr, key=lambda x: x[1]))   # [3,7,9]
# 2열이 가장 큰 행의 3번째 요소
print(max(arr, key=lambda x: x[1])[2])   # 9

 

# ----------------------------------------------------------

a = [1, 6, 2, 5, 2, 7, 2, 8, 9, 11, 5, 26]
result = list(map(lambda x : x**2, a))  # 제곱시키기
print(result)
result2 = list(map(lambda x : str(x) if x % 2 == 0 else x, a))   # 짝수인 것은 string 타입으로 cast 아니면 단순히 반환
print(result2)

b = [12, 16, 24, 5, 20, 27, 12, 8, 9, 110, 51, 26]
result3 = list(map(lambda x, y : x + y, a, b))  # 리스트 자료형 두 개 받아서 연산
print(result3)

# ----------------------------------------------------------

full_name = lambda first, last: f'Full name: {first.title()} {last.title()}'
full_name('Fname', 'Name')
 

# ----------------------------------------------------------

def square(x):
    return lambda : x*x

listOfLambdas = [square(i) for i in [1,2,3,4,5]]
for f in listOfLambdas:
    print(f())

listOfLambdas = [lambda i=i: i*i for i in range(1, 6)]
for f in listOfLambdas:
   print(f())

 

 

 

Posted by 오늘보다 나은 내일