🐹 μ½œλ°±ν•¨μˆ˜μ™€ λžŒλ‹€ν•¨μˆ˜

λ―Όλ‹¬νŒ½μ΄μš°μœ Β·2024λ…„ 6μ›” 2일

🐹 파이썬 기초

λͺ©λ‘ 보기
15/19

πŸ’‘ 1. 콜백 ν•¨μˆ˜(Callback Function)

콜백 ν•¨μˆ˜λŠ” λ‹€λ₯Έ ν•¨μˆ˜μ— 인자둜 μ „λ‹¬λ˜μ–΄, μ–΄λ–€ μ΄λ²€νŠΈλ‚˜ νŠΉμ • μ‘°κ±΄μ—μ„œ μ‹€ν–‰λ˜κ²Œ ν•˜λŠ” ν•¨μˆ˜λ₯Ό λ§ν•œλ‹€. 콜백 ν•¨μˆ˜λŠ” 주둜 비동기 μž‘μ—…μ΄λ‚˜ νŠΉμ • 이벀트 λ°œμƒ μ‹œμ μ— μ–΄λ–€ λ™μž‘μ„ μˆ˜ν–‰ν•  λ•Œ μ‚¬μš©λœλ‹€.

def callback_func(func):
  for i in range(5):
    func()
    
def print_hello():
  print('μ•ˆλ…•ν•˜μ„Έμš”! 파이썬')
  
print_hello()
> μ•ˆλ…•ν•˜μ„Έμš”! 파이썬
callback_func(print_hello) 
> μ•ˆλ…•ν•˜μ„Έμš”! 파이썬
μ•ˆλ…•ν•˜μ„Έμš”! 파이썬
μ•ˆλ…•ν•˜μ„Έμš”! 파이썬
μ•ˆλ…•ν•˜μ„Έμš”! 파이썬
μ•ˆλ…•ν•˜μ„Έμš”! 파이썬
def callback_func(func, num):
  for i in range(num):
    func(i)
    
def print_hello(num):
  print('μ•ˆλ…•ν•˜μ„Έμš” 파이썬!', num)
  
def print_hi(num):
  print('μ•ˆλ…• 파이썬!', num)
  
callback_func(print_hello, 3)
>  ]
callback_func(print_hello, 3)
μ•ˆλ…•ν•˜μ„Έμš” 파이썬! 0
μ•ˆλ…•ν•˜μ„Έμš” 파이썬! 1
μ•ˆλ…•ν•˜μ„Έμš” 파이썬! 2
callback_func(print_hi, 5)
> μ•ˆλ…• 파이썬! 0
μ•ˆλ…• 파이썬! 1
μ•ˆλ…• 파이썬! 2
μ•ˆλ…• 파이썬! 3
μ•ˆλ…• 파이썬! 4

πŸ’‘ 2. λžŒλ‹€ ν•¨μˆ˜(Lambda Function)

λžŒλ‹€ ν•¨μˆ˜λŠ” νŒŒμ΄μ¬μ—μ„œ 읡λͺ…μ˜ κ°„λ‹¨ν•œ ν•¨μˆ˜λ₯Ό μƒμ„±ν•˜κΈ° μœ„ν•œ νŠΉλ³„ν•œ ꡬ문이닀. '읡λͺ…μ˜ ν•¨μˆ˜'λΌλŠ” 것은 ν•¨μˆ˜μ— κ³ μœ ν•œ 이름이 μ§€μ •λ˜μ§€ μ•Šμ•˜μŒμ„ μ˜λ―Έν•œλ‹€. λžŒλ‹€ ν•¨μˆ˜λŠ” 일반적인 ν•¨μˆ˜(defλ₯Ό μ‚¬μš©ν•˜μ—¬ μ •μ˜)μ™€λŠ” 달리, ν•œ μ€„λ‘œ ν‘œν˜„λ˜λŠ” μ§§κ³  κ°„κ²°ν•œ ν•¨μˆ˜λ₯Ό 생성할 λ•Œ 주둜 μ‚¬μš©λœλ‹€.

def square(x):
  return x**2
  
print(square(5))
> 25
square = lambda x: x**2

print(square(5))
> 25
(lambda x: x**2)(5)
> 25
people = [
    {'name':'유민콩', 'age' : 3},
    {'name':'μœ μ„ μ½©', 'age' : 7},
    {'name':'μœ ν˜„μ½©', 'age' : 5}
]
def sort_age(x):
  return x['age']

def sort_age(x):
  return x['age']
  
print(sort_age(people[0]))
> 3
sorted_people = sorted(people, key = sort_age) # keyμ—λŠ” μ½œλ°±ν•¨μˆ˜λ‘œ μ €μž₯ (ν•¨μˆ˜μ˜ μ΄λ¦„μœΌλ‘œ μ €μž₯)
print(sorted_people) # λ‚˜μ΄λ³„λ‘œ μ˜€λ¦„μ°¨μˆœ
# 그런데 sort_age ν•¨μˆ˜λŠ” 이 ν•¨μˆ˜ 외에 쓰일 일이 μ—†μŒ -> λ©”λͺ¨λ¦¬ λ‚­λΉ„
# 이럴 λ•Œ λžŒλ‹€ ν•¨μˆ˜ μ‚¬μš©
> [{'name': '유민콩', 'age': 3}, {'name': 'μœ ν˜„μ½©', 'age': 5}, {'name': 'μœ μ„ μ½©', 'age': 7}]

πŸ’‘ 3. λžŒλ‹€κ°€ μœ μš©ν•˜κ²Œ μ“°μ΄λŠ” λŒ€ν‘œμ μΈ ν•¨μˆ˜

3-1. filter ν•¨μˆ˜

filter()λŠ” 파이썬의 λ‚΄μž₯ ν•¨μˆ˜λ‘œ, μ£Όμ–΄μ§„ ν•¨μˆ˜μ˜ 쑰건을 λ§Œμ‘±ν•˜λŠ” ν•­λͺ©λ§ŒμœΌλ‘œ 이루어진 μ΄ν„°λ ˆμ΄ν„°λ₯Ό λ°˜ν™˜ν•œλ‹€. 이 ν•¨μˆ˜λŠ” 주둜 λ¦¬μŠ€νŠΈλ‚˜ λ‹€λ₯Έ 순차적인 데이터 νƒ€μž…μ—μ„œ νŠΉμ • 쑰건을 λ§Œμ‘±ν•˜λŠ” ν•­λͺ©λ“€λ§Œμ„ 필터링할 λ•Œ μ‚¬μš©λœλ‹€.

li = [2, 5, 7, 10, 15, 17, 20, 22, 25, 28]
def even(n):
  if n % 2 == 0:
    return True
  else:
    return False
    
filter(even, li) # μ΄ν„°λ ˆμ΄ν„° 객체둜 좜λ ₯
> <filter at 0x7b4fd9a656c0>
result = list(filter(even, li)) # μ΄ν„°λ ˆμ΄ν„° 객체λ₯Ό 리슀트둜 λ³€ν™˜ν•΄μ£Όλ©΄ 됨
print(result)
> [2, 10, 20, 22, 28]
list(filter(lambda n: n % 2 == 0 , li)) # λžŒλ‹€ ν•¨μˆ˜λ‘œ 더 κ°„λ‹¨ν•˜κ²Œ μ‚¬μš© κ°€λŠ₯
> [2, 10, 20, 22, 28]

3-2. map ν•¨μˆ˜

map()λŠ” 파이썬의 λ‚΄μž₯ ν•¨μˆ˜λ‘œ, μ£Όμ–΄μ§„ ν•¨μˆ˜λ₯Ό μ΄ν„°λŸ¬λΈ”μ˜ λͺ¨λ“  ν•­λͺ©μ— μ μš©ν•˜μ—¬ κ²°κ³Όλ₯Ό λ°˜ν™˜ν•˜λŠ” μžλ ˆμ΄ν„°λ₯Ό μƒμ„±ν•œλ‹€. 이 ν•¨μˆ˜λŠ” 주둜 λ¦¬μŠ€νŠΈλ‚˜ λ‹€λ₯Έ 순차적인 데이터 νƒ€μž…μ˜ ν•­λͺ© 각각에 ν•¨μˆ˜λ₯Ό μ μš©ν•  λ•Œ μ‚¬μš©λœλ‹€.

numbers = [1, 2, 3, 4, 5]
squared_numbers = list(map(lambda x: x**2, numbers)) # μ•žμ˜ μžˆλŠ” 곡식을 뒀에 μžˆλŠ” λͺ¨λ“  μš”μ†Œμ— μ μš©ν•΄μ„œ 리턴. filter ν•¨μˆ˜μ™€ λ§ˆνƒ„κ°€μ§€λ‘œ μ΄ν„°λ ˆμ΄ν„°λ₯Ό λ¦¬ν„΄ν•˜κΈ° 떄문에 μ›ν•˜λŠ” μžλ£Œν˜•μœΌλ‘œ λ³€κ²½ν•΄μ€˜μ•Όν•¨
print(squared_numbers)
> [1, 4, 9, 16, 25]
li1 = [1, 2, 3]
li2 = [4, 5, 6]
sum = list(map(lambda x, y: x+y, li1, li2))
print(sum)
> [5, 7, 9]
words = ['apple', 'banna', 'cherry']
upper_words = list(map(lambda x: x.upper(), words))
print(upper_words)
> ['APPLE', 'BANNA', 'CHERRY']
profile
μ–΄λ–»κ²Œ ν–„μŠ€ν„°κ°€ 개발자

0개의 λŒ“κΈ€