|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 只为 于 2017-8-29 21:48 编辑
1、lambda表达式
- >>> g = lambda x : 2 * x +1
- >>> g(3)
- 7
- >>> type(g)
- <class 'function'>
- >>> lambda x : 2 * x +1
- <function <lambda> at 0x00000000034F3D08>
复制代码
2、lambda表达式的作用
1)python写一些执行代码是,使用lambda可以省下定义函数过程
2)对于一些比较抽象并且整个程序执行下来只需要调用一两次的函数,使用lambda不需要考虑命名的问题
3)简化代码的可读性,不用阅读函数时跳到def定义部分
3、两个牛逼的BIF
1)filter() 过滤器:过滤掉加的
查看帮助:help(filter)
- >>> help(filter)
- Help on class filter in module builtins:
- class filter(object)
- | filter(function or None, iterable) --> filter object
- |
- | Return an iterator yielding those items of iterable for which function(item)
- | is true. If function is None, return the items that are true.
复制代码
eg.
- >>> filter(None,[1,True,False,'',{},None,[]])
- <filter object at 0x00000000034F1B70>
- >>> list(filter(None,[1,True,False,'',{},None,[]]))
- [1, True]
复制代码
- >>> list(filter(lambda x : x%2,range(10)))
- [1, 3, 5, 7, 9]
复制代码
2)map() 映射
- >>> help(map)
- Help on class map in module builtins:
- class map(object)
- | map(func, *iterables) --> map object
- |
- | Make an iterator that computes the function using arguments from
- | each of the iterables. Stops when the shortest iterable is exhausted.
- >>> list(map(lambda x : x *2, range(9)))
- [0, 2, 4, 6, 8, 10, 12, 14, 16]
复制代码
|
评分
-
查看全部评分
|