匿名函数lambda
2015-06-20 22:04
357 查看
lambda x: x % 3 == 0
Is the same as
def by_three(x): return x % 3 == 0
Only we don't need to actually give the function a name; it does its work and returns a value without one. That's why the function the lambda creates is an anonymous function.
当不需要给一个函数名字时多用lambda
When we pass the lambda to
filter,
filteruses
the lambda to determine what to filter, and the second argument (
my_list, which
is just the numbers 0 – 15) is the list it does the filtering on.
my_list = range(16) print filter(lambda x: x % 3 == 0, my_list)
结果
[0, 3, 6, 9, 12, 15]
Lambda functions are defined using the following syntax:
my_list = range(16) filter(lambda x: x % 3 == 0, my_list)
Lambdas are useful when you need a quick function to do some work for you.
If you plan on creating a function you'll use over and over, you're better off using
defand giving that function a name.
相关文章推荐
- 对于 基类引用指向导出类对象 情况的理解
- Oracle的SQL优化一
- iOS手机应用开发原型模板及开发流程
- .net开发资源精华
- hash code、equals和“==”三者的关系
- 是是是
- No such table xxx错误的原因、解决办法:
- BestCoder Round #45
- PageRank算法简介及Map-Reduce实现
- AspNetPager
- [BZOJ2730]HNOI2012矿场搭建|割点
- idea打包可执行文件
- CSMA/CD
- ADSL拨号上网,拨完了获得的地址IP跟网关一样!
- unity平台的预处理
- 6月20日
- Jquery在两个HTML页面之间传递参数
- Android仓库管理系统(单机版)
- [BZOJ4002]JLOI2015有意义的字符串|矩阵乘法
- BP算法与公式推导