๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ Python

์ดํŽ™ํ‹ฐ๋ธŒ ํŒŒ์ด์ฌ 4์žฅ - ์ปดํ”„๋ฆฌํ—จ์…˜๊ณผ ์ œ๋„ˆ๋ ˆ์ดํ„ฐ

by dev.py 2025. 3. 18.

Better way 27 map๊ณผ filter ๋Œ€์‹  ์ปดํ”„๋ฆฌํ—จ์…˜์„ ์‚ฌ์šฉํ•˜๋ผ

# map๊ณผ filter ์‚ฌ์šฉ
numbers = [1, 2, 3, 4, 5, 6]
even_numbers = list(filter(lambda x: x % 2 == 0, numbers))
even_squares = list(map(lambda x: x ** 2, even_numbers))

# ์ข‹์€ ์˜ˆ์‹œ - ๋ฆฌ์ŠคํŠธ ์ปดํ”„๋ฆฌํ—จ์…˜
even_squares = [x ** 2 for x in numbers if x % 2 == 0]

 

Better way 28 ์ปดํ”„๋ฆฌํ—จ์…˜ ๋‚ด๋ถ€์— ์ œ์–ด ํ•˜์œ„ ์‹์„ ์„ธ ๊ฐœ ์ด์ƒ ์‚ฌ์šฉํ•˜์ง€ ๋ง๋ผ

# ์•ˆ ์ข‹์€ ์˜ˆ์‹œ
matrix = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
flattened = [num for layer in matrix for row in layer for num in row if num % 2 == 0]

# ์ข‹์€ ์˜ˆ์‹œ
matrix = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
even_numbers = []
for layer in matrix:
    for row in layer:
        for num in row:
            if num % 2 == 0:
                even_numbers.append(num)

 

Better way 29 ๋Œ€์ž…์‹์„ ์‚ฌ์šฉํ•ด ์ปดํ”„๋ฆฌํ—จ์…˜ ์•ˆ์—์„œ ๋ฐ˜๋ณต ์ž‘์—…์„ ํ”ผํ•˜๋ผ

# ์•ˆ ์ข‹์€ ์˜ˆ์‹œ
words = ["hello", "world", "python"]
lengths = [len(word) for word in words if len(word) > 4]

# ์ข‹์€ ์˜ˆ์‹œ
lengths = [length for word in words if (length := len(word)) > 4]

Better way 30 ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ธฐ๋ณด๋‹ค๋Š” ์ œ๋„ˆ๋ ˆ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๋ผ

# ๋ฆฌ์ŠคํŠธ ๋ฐ˜ํ™˜
def squares(n):
    return [x ** 2 for x in range(n)]
    
# ์ œ๋„ˆ๋ ˆ์ดํ„ฐ ์‚ฌ์šฉ
def squares(n):
    for x in range(n):
        yield x ** 2

Better way 31 ์ธ์ž์— ๋Œ€ํ•ด ์ดํ„ฐ๋ ˆ์ด์…˜ํ•  ๋•Œ๋Š” ๋ฐฉ์–ด์ ์ด ๋ผ๋ผ

# ์•ˆ ์ข‹์€ ์˜ˆ์‹œ
def process(iterator):
    first_pass = list(iterator)
    second_pass = list(iterator)  # ๋นˆ ๋ฆฌ์ŠคํŠธ ๋ฐ˜ํ™˜
    
# ์ดํ„ฐ๋ ˆ์ดํ„ฐ๋ฅผ ๋ฆฌ์ŠคํŠธ๋กœ ๋ณ€ํ™˜
def process(iterable):
    data = list(iterable)
    first_pass = data
    second_pass = data

 

Better way 32 ๊ธด ๋ฆฌ์ŠคํŠธ ์ปดํ”„๋ฆฌํ—จ์…˜๋ณด๋‹ค๋Š” ์ œ๋„ˆ๋ ˆ์ดํ„ฐ ์‹์„ ์‚ฌ์šฉํ•˜๋ผ

# ๋ฆฌ์ŠคํŠธ ์ปดํ”„๋ฆฌํ—จ์…˜ (๋งŽ์€ ๋ฉ”๋ชจ๋ฆฌ ์ฐจ์ง€)
squares = [x ** 2 for x in range(10**6)]

# ์ œ๋„ˆ๋ ˆ์ดํ„ฐ ( next() ๋ฅผ ํ˜ธ์ถœ ํ•  ๋•Œ ๋งˆ๋‹ค ํ˜ธ์ถœ)
squares = (x ** 2 for x in range(10**6))

Better way 33 yield from์„ ์‚ฌ์šฉํ•ด ์—ฌ๋Ÿฌ ์ œ๋„ˆ๋ ˆ์ดํ„ฐ๋ฅผ ํ•ฉ์„ฑํ•˜๋ผ

# ์•ˆ์ข‹์€ ์˜ˆ์‹œ ๋ฐ˜๋ณต๋ฌธ ์‚ฌ์šฉ
def chain(iter1, iter2):
    for item in iter1:
        yield item
    for item in iter2:
        yield item

# ์ข‹์€ ์˜ˆ์‹œ - yield from ์‚ฌ์šฉ
def chain(iter1, iter2):
    yield from iter1
    yield from iter2

 

Better way 34 send๋กœ ์ œ๋„ˆ๋ ˆ์ดํ„ฐ์— ๋ฐ์ดํ„ฐ๋ฅผ ์ฃผ์ž…ํ•˜์ง€ ๋ง๋ผ

# ์•ˆ ์ข‹์€ ์˜ˆ์‹œ ์™ธ๋ถ€์—์„œ send()๋กœ  ๊ฐ’์„ ๋ฐ›์•„์˜ด
def message_generator():
    message = "Start"
    while True:
        received = yield message  # ์™ธ๋ถ€์—์„œ ๊ฐ’์„ ๋ฐ›์•„์˜ด
        if received is not None:
            message = f"Received: {received}"

msg_gen = message_generator()
print(next(msg_gen))  # Start
print(msg_gen.send("Hello"))  # Received: Hello

# ๋” ๋‚˜์€ ์˜ˆ์‹œ - ํ•จ์ˆ˜ ์ธ์ž๋กœ ๋ฐ์ดํ„ฐ ์ „๋‹ฌ
def process_message(message, additional):
    return f"Processed: {message} with {additional}"

print(process_message("Hello", "Extra Data"))  # Processed: Hello with Extra Data

Better way 35 ์ œ๋„ˆ๋ ˆ์ดํ„ฐ ์•ˆ์—์„œ throw๋กœ ์ƒํƒœ๋ฅผ ๋ณ€ํ™”์‹œํ‚ค์ง€ ๋ง๋ผ

# ์•ˆ ์ข‹์€ ์˜ˆ์‹œ - throw()๋ฅผ ์‚ฌ์šฉ
def error_generator():
    try:
        while True:
            yield
    except ValueError:
        print("Error received!")

gen = error_generator()
next(gen)
gen.throw(ValueError)  # Error received!

# ์ข‹์€ ์˜ˆ์‹œ
def safe_generator():
    yield "Processing..."
    raise ValueError("Error occurred")

try:
    gen = safe_generator()
    print(next(gen))
    print(next(gen))  # ์—ฌ๊ธฐ์„œ ์˜ˆ์™ธ ๋ฐœ์ƒ
except ValueError as e:
    print("Handled error:", e)

Better way 36 ์ดํ„ฐ๋ ˆ์ดํ„ฐ๋‚˜ ์ œ๋„ˆ๋ ˆ์ดํ„ฐ๋ฅผ ๋‹ค๋ฃฐ ๋•Œ๋Š” itertools๋ฅผ ์‚ฌ์šฉํ•˜๋ผ

# ์•ˆ ์ข‹์€ ์˜ˆ์‹œ - ์ˆ˜๋™ ๋ฐ˜๋ณต ๊ตฌํ˜„
def repeat_n_times(value, n):
    return [value for _ in range(n)]

print(repeat_n_times("Hello", 3))  # ['Hello', 'Hello', 'Hello']


# ์ข‹์€ ์˜ˆ์‹œ - itertools ์‚ฌ์šฉ
from itertools import repeat
print(list(repeat("Hello", 3)))  # ['Hello', 'Hello', 'Hello']