- 用lambda代替一种简单函数
g=lambda x:x+1
print(g(1)) //就是会给出x+1的值
def g(x):
return x+1 //类似于这种
- 也可以在后面直接添加参数去做
>>>print(lambda x:x.startwith('b'))('b')
True
- 与map函数一起用
sentence = "Hello World"
words = sentence.split()
print(type(words)) //输出是list
lengths=map(lambda x:len(x),words) //这个lengths是map object
print(list(lengths))
- 与apply一起用
df['text_len']=df['text'].apply(lambda x:len(x.split(' '))
apply和map的作用非常类似
但是apply一般是用在dataframe上
map是用在list上