python - lambda 函数使用
# if we need it only once and it's quite simple def make_incrementor(n): return lambda x: x + n f = make_incrementor(2) print f(3)
# filter, map, reduce foo = [2, 18, 9, 22, 17, 24, 8, 12, 27] print filter(lambda x: x % 3 == 0, foo) print map(lambda x: x * 2 + 10, foo) print reduce(lambda x, y: x + y, foo)
nums = range(2, 50) for i in range(2, 8): nums = filter(lambda x: x == i or x % i, nums) print nums
print map(lambda word: len(word), "it is raining cats and dogs".split())
import commands print map(lambda x: x.split()[2],commands.getoutput('mount -v').splitlines()) # or lines = commands.getoutput('mount -v').splitlines() points = map(lambda line: line.split()[2], lines) print points