f = lambda x: x + 1
print(f(1))
2
g = lambda x, y: x + y
print(g(1, 2))
3
incr = lambda x, inc = 1: x + inc
print(incr(10))#inc 기본 인수 값으로 1 사용
print(incr(10, 5))
11 15
vargs = lambda x, *args: args
print(vargs(1, 2, 3, 4, 5))
# 이전 jupyter notebook 참고; "넣어주는 인수 값들 중 일반 인수에 할당되는 값을 제외한 나머지 값들을 지닌 튜플 객체가 할당된다."
print(type(vargs))
print(type(vargs(1, 2, 3, 4, 5)))
(2, 3, 4, 5) <class 'function'> <class 'tuple'>
def f1(x):
return x * x + 3 * x - 10
def f2(x):
return x * x * x
def g(func):
return [func(x) for x in range(-10, 10)]
print(g(f1))
print(g(f2))
[60, 44, 30, 18, 8, 0, -6, -10, -12, -12, -10, -6, 0, 8, 18, 30, 44, 60, 78, 98] [-1000, -729, -512, -343, -216, -125, -64, -27, -8, -1, 0, 1, 8, 27, 64, 125, 216, 343, 512, 729]
def g(func):
return [func(x) for x in range(-10, 10)]
print(g(lambda x: x * x + 3 * x - 10))
print(g(lambda x: x * x * x))
[60, 44, 30, 18, 8, 0, -6, -10, -12, -12, -10, -6, 0, 8, 18, 30, 44, 60, 78, 98] [-1000, -729, -512, -343, -216, -125, -64, -27, -8, -1, 0, 1, 8, 27, 64, 125, 216, 343, 512, 729]
# 더하기, 빼기, 곱하기, 나누기에 해당하는 람다 함수 리스트 정의
func_list = [
lambda x, y: x + y,
lambda x, y: x - y,
lambda x, y: x * y,
lambda x, y: x / y
]
def menu():
print("0. add")
print("1. sub")
print("2. mul")
print("3. div")
print("4. quit")
return int(input('Select menu:'))
while 1:
sel = menu()
if sel < 0 or sel > len(func_list):
continue
if sel == len(func_list):
break
x = int(input('First operand:'))
y = int(input('Second operand:'))
print('Result =', func_list[sel](x,y), end="\n\n")
0. add 1. sub 2. mul 3. div 4. quit Select menu:1 First operand:10 Second operand:2 Result = 8 0. add 1. sub 2. mul 3. div 4. quit Select menu:2 First operand:10 Second operand:10 Result = 100 0. add 1. sub 2. mul 3. div 4. quit Select menu:4
map, filter, reduce
내장 함수function
map(function, seq)
def f(x):
return x * x
X = [1, 2, 3, 4, 5]
m = map(f, X)
print(type(m))
<class 'map'>
def f(x):
return x * x
X = [1, 2, 3, 4, 5]
Y = list(map(f, X))
print(Y)
[1, 4, 9, 16, 25]
def f(x):
return x * x
X = [1, 2, 3, 4, 5]
Y = []
for x in X:
y = f(x)
Y.append(y)
print(Y)
[1, 4, 9, 16, 25]
X = [1, 2, 3, 4, 5]
print(list(map(lambda x: x * x, X)))
[1, 4, 9, 16, 25]
Y = list(map(lambda x: x * x + 4 * x + 5, range(10)))
print(Y)
[5, 10, 17, 26, 37, 50, 65, 82, 101, 122]
y = list(map(lambda x: len(x), ["Hello", "Python", "Programming"]))
print(y)
[5, 6, 11]
filter(function, seq)
print(list(filter(lambda x: x > 2, [1, 2, 3, 34])))
[3, 34]
y = []
for x in [1, 2, 3, 34]:
if x > 2:
y.append(x)
print(y)
[3, 34]
print(list(filter(lambda x: x % 2, [1, 2, 3, 4, 5, 6])))
[1, 3, 5]
print(list(filter(lambda x: x % 2 - 1, [1, 2, 3, 4, 5, 6])))
[2, 4, 6]
def F():
x = 1
print(list(filter(lambda a: a > x, range(-5, 5))))
F()
[2, 3, 4]
(python2)
(python3)
print(filter(lambda x: x > 2, [1, 2, 3, 34]))
print(filter(lambda x: x > 2, (1, 2, 3, 34)))
print(filter(lambda x: x < 'a', 'abcABCdefDEF'))
<filter object at 0x1102ea668> <filter object at 0x1102ea240> <filter object at 0x1102ea668>
print(list(filter(lambda x: x > 2, [1, 2, 3, 34])))
print(tuple(filter(lambda x: x > 2, (1, 2, 3, 34))))
print(list(filter(lambda x: x < 'a', 'abcABCdefDEF')))
[3, 34] (3, 34) ['A', 'B', 'C', 'D', 'E', 'F']
reduce (function, seq[, initial])
from functools import reduce
reduce
returns a single value.)from functools import reduce
print(reduce(lambda x, y: x + y, [1, 2, 3, 4, 5]))
15
단계 | x | y | reduce |
---|---|---|---|
1 | 0 | 1 | 1 |
2 | 1 | 2 | 3 |
3 | 3 | 3 | 6 |
4 | 6 | 4 | 10 |
5 | 10 | 5 | 15 |
print(reduce(lambda x, y: x + y, [1, 2, 3, 4, 5], 100))
115
print(reduce(lambda x, y: x + y * y, range(1, 11), 0))
385
x = 0
for y in range(1, 11):
x = x + y * y
print(x)
385
print(reduce(lambda x, y: y + x, 'abcde'))
edcba
단계 | x | y | reduce |
---|---|---|---|
1 | '' | 'a' | 'a' |
2 | 'a' | 'b' | 'ba' |
3 | 'ba' | 'c' | 'cba' |
4 | 'cba' | 'd' | 'dcba' |
5 | 'dcba' | 'e' | 'edcba' |
lis = [1, 3, 5, 6, 2]
print("The maximum element of the list is : ",end="")
print(reduce(lambda a, b: a if a > b else b, lis))
The maximum element of the list is : 6
참고 문헌: 파이썬(열혈강의)(개정판 VER.2), 이강성, FreeLec, 2005년 8월 29일