๐Ÿ†™ Python 1. ๋žŒ๋‹ค(lambda)

matisseยท2020๋…„ 3์›” 30์ผ
0

Python Advanced

๋ชฉ๋ก ๋ณด๊ธฐ
1/3

map ํ•จ์ˆ˜๋ฅผ ๊ฒ€์ƒ‰ํ•˜๋‹ค๊ฐ€ filter, reduce๊นŒ์ง€ ํ•จ๊ป˜ ์ •๋ฆฌ๋œ ๊ธ€์„ ๋ดค๋‹ค. ๊ทผ๋ฐ lambda์™€ ๊ฐ™์ด ์“ฐ์ธ๋‹ค๊ณ  ํ•ด์„œ, ๊ด€๋ จ๋œ ๋‚ด์šฉ์„ ํ•จ๊ป˜ ์ •๋ฆฌํ•ด๋ณด๋ ค๊ณ  ํ•œ๋‹ค.

map, filter, reduce๋Š” javascript ์ฝ”๋“œ์นด๋ฐ๋ฏธ ๊ฐ•์˜์—์„œ iterator ๋ฐฐ์šธ ๋•Œ ๋‚˜์™”๋˜ ๊ฐœ๋…์ธ๋ฐ ๋ญ”๊ฐ€ ๋Š๋‚Œ์ด ๋‹ค๋ฅด๋‹ค ๐Ÿ˜“

1. lambda

๋žŒ๋‹ค ํ˜•์‹์€ ํ•จ์ˆ˜๋ฅผ ๋”ฑ ํ•œ ์ค„๋งŒ์œผ๋กœ ๋งŒ๋“ค๊ฒŒ ํ•ด์ค€๋‹ค. ์‚ฌ์šฉํ•  ๋•Œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ์จ์ฃผ๋ฉด ๋œ๋‹ค.

lambda ์ธ์ž : ํ‘œํ˜„์‹

def hap(x, y):
   return x + y

hap(10, 20) # result: 30

(lambda x,y: x + y)(10, 20) # result: 30

2. map

map์˜ ์ฒซ ๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” ํ•จ์ˆ˜, ๋‘ ๋ฒˆ์งธ๋กœ๋Š” iterable์ด ๋“ค์–ด๊ฐ„๋‹ค. ๊ทธ๋ฆฌ๊ณ  iterable ๋ฐ์ดํ„ฐ๋ฅผ ์ฒซ ๋ฒˆ์งธ ์ธ์ž์ธ ํ•จ์ˆ˜์— ๊ฐ๊ฐ ์ „๋‹ฌํ•˜์—ฌ ๊ฒฐ๊ณผ๊ฐ’์„ ๋ฐ›๋Š”๋‹ค.

๊ฒฐ๊ณผ๊ฐ’์˜ ๊ฒฝ์šฐ python 2์—์„œ๋Š” listํ˜•์„ ๋ฐ˜ํ™˜ํ–ˆ์ง€๋งŒ, 3์—์„œ๋Š” generatorํ˜•์œผ๋กœ ๋ฐ˜ํ™˜ํ•˜๋„๋ก ๋ณ€๊ฒฝ๋˜์—ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๋‹จ์ˆœ map ํ•จ์ˆ˜๋งŒ ์“ฐ๊ฒŒ ๋  ๊ฒฝ์šฐ ์ธํ„ฐํ”„๋ฆฌํ„ฐ์— <map object at 0x10f9d6828> ์ด๋Ÿฐ ์‹์œผ๋กœ ๊ฒฐ๊ณผ๊ฐ’์ด ์•„๋‹Œ iterator๋ฅผ returnํ•œ๋‹ค.

๊ทธ๋ž˜์„œ list๋กœ ๋ฐ›๊ณ  ์‹ถ๋‹ค๋ฉด list() ํ•จ์ˆ˜๋กœ ๊ฐ์‹ธ์„œ type์„ ๋ณ€๊ฒฝํ•ด์•ผ ํ•œ๋‹ค.

map(function_to_apply, list_of_inputs)
#๋„์–ด์“ฐ๊ธฐ๋กœ input๋˜๋Š” ์ •์ˆ˜ ๊ฐ’ ๋‘๊ฐœ๋ฅผ ๋ฐ›๋Š” ๋ณ€์ˆ˜

a,b = map(int, input().split())

์ฒซ ๋ฒˆ์งธ ์ธ์ž๋กœ ๋“ค์–ด์˜ค๋Š” ํ•จ์ˆ˜๋ฅผ ๋ฐ”๋กœ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด lambda ํ˜•์‹์„ ์ด์šฉํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋‹ค.

map(lambda x: x ** 2, range(5))             # ํŒŒ์ด์ฌ 2
[0, 1, 4, 9, 16]  
>>> list(map(lambda x: x ** 2, range(5)))     # ํŒŒ์ด์ฌ 2 ๋ฐ ํŒŒ์ด์ฌ 3
[0, 1, 4, 9, 16]

1.1 generator

์‰ฝ๊ฒŒ ๋งํ•˜๋ฉด iterator๋ฅผ returnํ•˜๋Š” ํ•จ์ˆ˜๋‹ค. for loop์—์„œ ์—ฐ์†๋˜๋Š” ๊ฐ’๋“ค์„ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๊ฒŒ ๋งŒ๋“ค๊ณ , next() ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ํ•œ๋ฒˆ์— ํ•˜๋‚˜์”ฉ ๊ฒ€์ƒ‰๋  ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์—์„œ ์ผ๋ฐ˜ ํ•จ์ˆ˜์™€ ๋‹ค๋ฅด์ง€ ์•Š์ง€๋งŒ, yield statment๋ฅผ ๊ฐ–๊ณ  ์žˆ๋‹ค๋Š” ๊ฒƒ์ด ํฐ ์ฐจ์ด์ ์ด๋‹ค.

iterator๋Š” ํด๋ž˜์Šค์— __iter__, __next__ ๋˜๋Š” __getitem__ ๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•ด์•ผ ํ•˜์ง€๋งŒ ์ œ๋„ˆ๋ ˆ์ดํ„ฐ๋Š” ํ•จ์ˆ˜ ์•ˆ์—์„œ yield๋ผ๋Š” ํ‚ค์›Œ๋“œ๋งŒ ์‚ฌ์šฉํ•˜๋ฉด ๋์ด๋‹ค.

generator | A function which returns an iterator. It looks like a normal function except that it contains yield statements for producing a series of values usable in a for-loop or that can be retrieved one at a time with the next() function.

Each yield temporarily suspends processing, remembering the location execution state (including local variables and pending try-statements). When the generator resumes, it picks-up where it left-off (in contrast to functions which start fresh on every invocation).

1.2 yield statement

์ผ๋ฐ˜ ํ•จ์ˆ˜์™€ generator๋ฅผ ๊ตฌ๋ถ„์‹œ์ผœ์ฃผ๋Š” ๊ฐ€์žฅ ํ•ต์‹ฌ์ ์ธ ๋ถ€๋ถ„์ด๋‹ค. ๊ฐ yield๋Š” ์ผ์‹œ์ ์œผ๋กœ ํ”„๋กœ์„ธ์Šค๋ฅผ ๋ฉˆ์ถ”๊ณ , ์‹คํ–‰๋ถ€๋ฅผ ๊ธฐ์–ตํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  generator๊ฐ€ ๋‹ค์‹œ ์‚ฌ์šฉ๋  ๋•Œ ๋งˆ์ง€๋ง‰์œผ๋กœ ๋๋ƒˆ๋˜ ๋ถ€๋ถ„๋ถ€ํ„ฐ ์‹œ์ž‘ํ•œ๋‹ค.

์ œ๋„ˆ๋ ˆ์ดํ„ฐ๋Š” ํ•œ ๋ฒˆ ํ˜ธ์ถœ๋ ๋•Œ๋งˆ๋‹ค ํ•˜๋‚˜์˜ ๊ฐ’๋งŒ์„ ์ „๋‹ฌ(yield)ํ•œ๋‹ค. ์ฆ‰, ์•„๋ž˜์˜ #1๊นŒ์ง€๋Š” ์•„์ง ์•„๋ฌด๋Ÿฐ ๊ณ„์‚ฐ์„ ํ•˜์ง€ ์•Š๊ณ  ๋ˆ„๊ตฐ๊ฐ€๊ฐ€ ๋‹ค์Œ ๊ฐ’์— ๋Œ€ํ•ด์„œ ๋ฌผ์–ด๋ณด๊ธฐ๋ฅผ ๊ธฐ๋‹ค๋ฆฌ๊ณ  ์žˆ๋Š” ์ƒํƒœ์ธ ๊ฒƒ์ด๋‹ค.

def square_numbers(nums):
    for i in nums:
        yield i * i

my_nums = square_numbers([1, 2, 3, 4, 5])  #1

print my_nums

3. filter

ํ•จ์ˆ˜๊ฐ€ True๋กœ returnํ•œ ์š”์†Œ๋“ค ๋ชจ์•„ ๋ฆฌ์ŠคํŠธ๋กœ ๋งŒ๋“ ๋‹ค. for loop๋ฅผ ๋‹ฎ์•˜์ง€๋งŒ ๋‚ด์žฅ ํ•จ์ˆ˜์ด๊ณ  ์†๋„๊ฐ€ ๋น ๋ฅด๋‹ค.

number_list = range(-5, 5)
less_than_zero = list(filter(lambda x: x < 0, number_list))
print(less_than_zero)

# Output: [-5, -4, -3, -2, -1]

4. reduce

๋ฆฌ์ŠคํŠธ์—์„œ ์–ด๋–ค ๊ณ„์‚ฐ์„ ํ•œ ๋’ค ๊ฒฐ๊ณผ๋ฅผ ๋‚ด๋ณด๋‚ด๋Š”๋ฐ์— ๋งค์šฐ ์œ ์šฉํ•œ ํ•จ์ˆ˜๋‹ค.

product = 1
list = [1, 2, 3, 4]
for num in list:
    product = product * num

# product = 24

5. map ํ™œ์šฉ ๋ฌธ์ œ

๋ฐฑ์ค€ ์ฝ”๋“œ ๋ฌธ์ œ๋ฅผ (์ œ์ผ ์‰ฌ์šด๊ฑฐ๐Ÿ™„) ํ•˜๋‚˜๋งŒ ํ’€์–ด๋ณด๋ ค๊ณ  ํ•˜๋‹ค๊ฐ€ input ๋ฐ›๋Š”๊ฑฐ๋ฅผ ๋ชฐ๋ผ์„œ ์‚ฌ์‹ค ์ •๋ฆฌ๋ฅผ ์‹œ์ž‘ํ–ˆ๋‹ค. ๊ฐ’์„ ์‹คํ–‰ํ•˜๋Š” ๋‚ด์šฉ ์ž์ฒด๋Š” ์–ด๋ ต์ง€ ์•Š์€๋ฐ, ์ฒ˜์Œ์—” ๊ฐ’ ๋“ค์–ด์˜ค๋ฉด ๋‚ด์šฉ์„ ์‹คํ–‰ํ•˜๋„๋ก ํ•˜๋Š” ์ž…๋ ฅ ๋ถ€๋ถ„์ด ์ž˜ ์ดํ•ด๊ฐ€ ์•ˆ ๊ฐ”๋‹ค.

๋ฌธ์ œ(1330): ๋‘ ์ •์ˆ˜ A์™€ B๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, A์™€ B๋ฅผ ๋น„๊ตํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์ž…๋ ฅ ์ฒซ์งธ ์ค„์— A์™€ B๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. A์™€ B๋Š” ๊ณต๋ฐฑ ํ•œ ์นธ์œผ๋กœ ๊ตฌ๋ถ„๋˜์–ด์ ธ ์žˆ๋‹ค.
์ถœ๋ ฅ ์ฒซ์งธ ์ค„์— ๋‹ค์Œ ์„ธ ๊ฐ€์ง€ ์ค‘ ํ•˜๋‚˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

A๊ฐ€ B๋ณด๋‹ค ํฐ ๊ฒฝ์šฐ์—๋Š” '>', ์ž‘์€ ๊ฒฝ์šฐ์—๋Š” '<', ๊ฐ™์€ ๊ฒฝ์šฐ '=='๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

์ œํ•œ -10,000 โ‰ค A, B โ‰ค 10,000

4.1 input

์ธํ„ฐํ”„๋ฆฌํ„ฐ์—์„œ ์ž…๋ ฅ ๊ฐ’์„ ๋ฐ›๋Š” ํ•จ์ˆ˜. terminal์—์„œ input() ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ๊ฐ’์„ ๋„ฃ์„ ์ˆ˜ ์žˆ๋„๋ก ์•ž์— ํ™”์‚ดํ‘œ >>>๊ฐ€ ์‚ฌ๋ผ์ง€๊ณ , ์ž…๋ ฅ ์‹œ ๊ฐ’์„ ๋ฐ”๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๋งŒ์•ฝ ๋ณ€์ˆ˜์— input() ํ•จ์ˆ˜๋ฅผ assign ํ•˜๋ฉด ๋ณ€์ˆ˜ ๊ฐ’์— input๊ฐ’์ด ๋“ค์–ด๊ฐ„๋‹ค.

4.2 split

parameter์— ๋„ฃ์€ ๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ๋ฌธ์ž์—ด์„ ๋‚˜๋ˆ„์–ด list๋กœ returnํ•˜๋Š” ํ•จ์ˆ˜๋‹ค. ๊ด„ํ˜ธ ์•ˆ์— ์•„๋ฌด๊ฒƒ๋„ ๋„ฃ์น˜ ์•Š์œผ๋ฉด ๊ณต๋ฐฑ์„ ๊ธฐ์ค€์œผ๋กœ ๋ฌธ์ž์—ด์„ ๋‚˜๋ˆ ์ค€๋‹ค.

a = "Life is too short"
a.split() # result: ['Life', 'is', 'too', 'short']

b = "a:b:c:d"
b.split(':') # result: ['a', 'b', 'c', 'd']

- join

split๊ณผ ๋ฐ˜๋Œ€๋กœ ๋ฌธ์ž์—ด ์‚ฌ์ด์— ์›ํ•˜๋Š” ๊ฐ’์„ ์ž…๋ ฅํ•ด์„œ ํ•˜๋‚˜๋กœ ํ•ฉ์ณ์ฃผ๋Š” ํ•จ์ˆ˜๋„ ์žˆ๋‹ค. join ํ•จ์ˆ˜๋Š” ๋ฌธ์ž์—ด๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์•ž์œผ๋กœ ๋ฐฐ์šธ ๋ฆฌ์ŠคํŠธ๋‚˜ ํŠœํ”Œ๋„ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

",".join('abcd')
'a,b,c,d'

 ",".join(['a', 'b', 'c', 'd'])
'a,b,c

6. ์ •๋‹ต์€?

a, b = map(int, input().split())

if a > b:
	print('>')
elif: a < b:
	print('<')
else:
	print('=')

0๊ฐœ์˜ ๋Œ“๊ธ€