lambda 与 python 高级函数的配套使用
filter函数 过滤
__author__ = "Tang"
# filter(lambda, [])
people = ['tanglaoer','chenlaosan_sb','linlaosi_sb','wanglaowu']
res = filter(lambda n:not n.endswith('_sb'),people)
print(list(res)) # ['tanglaoer', 'wanglaowu']
# filter(lambda,())
people = ('tanglaoer','chenlaosan_sb','linlaosi_sb','wanglaowu',)
res = filter(lambda n:not n.endswith('_sb'),people)
print(tuple(res)) # ('tanglaoer', 'wanglaowu')
map函数 遍历操作
# map(lambda,str)
msg = 'tanglaoer'
print(list(map(lambda x:x.upper(),msg))) #['T', 'A', 'N', 'G', 'L', 'A', 'O', 'E', 'R']
# map(lambda,[])
msg = ['tanglaoer','chenlaosan_sb','linlaosi_sb','wanglaowu']
print(list(map(lambda x:x.upper(),msg))) #['TANGLAOER', 'CHENLAOSAN_SB', 'LINLAOSI_SB', 'WANGLAOWU']
# map(lambda,())
msg = ('tanglaoer','chenlaosan_sb','linlaosi_sb','wanglaowu',)
print(tuple(map(lambda x:x.upper(),msg))) #('TANGLAOER', 'CHENLAOSAN_SB', 'LINLAOSI_SB', 'WANGLAOWU')
reduce函数 lambda接受两个参数
# reduce(lambda,[])
from functools import reduce
num_list = [1,2,3,100]
print(reduce(lambda x,y:x+y,num_list)) #
#reduce(lambda,())
num_list = (1,2,3,100,)
print(reduce(lambda x,y:x+y,num_list)) #