Python -- lambda, map, filter


lambda

f = lambda x : x * 2

f(5)


f = lambda x,y,z : x+y+z

f(2,1,3)

map

list(map(lambda x:x[0].upper()+x[1:].lower(), ['sQd', 'ZORO']))

#传入列表,首字母变大写,其余变小写

reduce

from functools import reduce

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

reduce(add, [1,2,3,4])
reduce(f, [x1, x2, x3, x4]) = f(f(f(x1, x2), x3), x4)

filter

list(filter(lambda n: n%2 == 1, [1,2,3,4,5]))

#保留奇数,舍弃偶数
list(filter(lambda s: s and s.strip(), ['S', '', None, 'b']))

#删除一个列表中的空元素

KEEP LEARNING!
原文地址:https://www.cnblogs.com/roronoa-sqd/p/4899175.html