f = lambda x: x + 1 print f(1) g = lambda x, y: x + y print g(1, 2) incr = lambda x, inc = 1: x + inc print incr(10) #inc 기본 인수 값으로 1 사용 print incr(10, 5) vargs = lambda x, *args: args print vargs(1,2,3,4,5) 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) 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) # 더하기, 빼기, 곱하기, 나누기에 해당하는 람다 함수 리스트 정의 func = [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 input('Select menu:') while 1: sel = menu() if sel < 0 or sel > len(func): continue if sel == len(func): break x = input('First operand:') y = input('Second operand:') print 'Result =', func[sel](x,y) def f(x): return x * x X = [1, 2, 3, 4, 5] Y = map(f, X) print Y 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 X = [1, 2, 3, 4, 5] print map(lambda x: x * x, X) Y = map(lambda x: x * x + 4 * x + 5, range(10)) print Y y = map(lambda x: len(x), ["Hello", "Python", "Programming"]) print y print filter(lambda x: x > 2, [1, 2, 3, 34]) y = [] for x in [1, 2, 3, 34]: if x > 2: y.append(x) print y print filter(lambda x: x % 2, [1, 2, 3, 4, 5, 6]) print filter(lambda x: x % 2 - 1, [1, 2, 3, 4, 5, 6]) def F(): x = 1 print filter(lambda a: a > x, range(-5, 5)) F() 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') print reduce(lambda x, y: x + y, [1, 2, 3, 4, 5]) print reduce(lambda x, y: x + y, [1, 2, 3, 4, 5], 100) print reduce(lambda x, y: x + y * y, range(1, 11), 0) x = 0 for y in range(1, 11): x = x + y * y print x print reduce(lambda x, y: y + x, 'abcde')