码迷,mamicode.com
首页 > 编程语言 > 详细

python学习之lambda匿名函数

时间:2015-04-29 23:06:55      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:

1

Python支持运行时使用“lambda”建立匿名函数(anonymous functions that are not bound to a name)。

python "lambda"和functional programming语言有区别,但是他非常强大经常拿来和诸如filter(),map(),reduce()
等经典概念结合。

以下示例普通函数和匿名函数:

1 In [113]: def normalFun (x): return x**2
2 
3 In [114]: print normalFun(8)
4 64
5 
6 In [115]: anonymousFun = lambda x:x**2
7 
8 In [116]: print anonymousFun(8)
9 64

普通函数和匿名函数运算结果都一样,但是匿名函数没有return语句,冒号后边

表达式就是返回值。

 

2 下面代码片段展示匿名函数用法,请保证python版本在2.2以上,因为需要支持嵌入作用域。

In [120]: def make_incrementor (n): return lambda x: x + n

In [121]: f = make_incrementor(2)

In [122]: g = make_incrementor(6)

In [123]: f
Out[123]: <function __main__.<lambda>>

In [124]: g
Out[124]: <function __main__.<lambda>>

In [125]: print(42)
42

In [126]: print f(42)
44

In [127]: print g(42)
48

注意,g,f 定义后类型显示位<function __main__.<lambda>>,说明此时g,f是匿名函数。

 

3 以下几个代码示范lambda和其他结合用法

In [133]: foo = [2, 18, 9, 22, 17, 24, 8, 12, 27]

In [134]: print filter(lambda x: x % 3 == 0, foo)
[18, 9, 24, 12, 27]

In [135]: ?filter
Type:        builtin_function_or_method
String form: <built-in function filter>
Namespace:   Python builtin
Docstring:
filter(function or None, sequence) -> list, tuple, or string

Return those items of sequence for which function(item) is true.  If
function is None, return the items that are true.  If sequence is a tuple
or string, return the same type, else return a list.

In [136]: print filter(foo)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-136-a3d764429161> in <module>()
----> 1 print filter(foo)

TypeError: filter expected 2 arguments, got 1

In [137]: print filter(None,foo)
[2, 18, 9, 22, 17, 24, 8, 12, 27]

 

          ?map
Type:        builtin_function_or_method
String form: <built-in function map>
Namespace:   Python builtin
Docstring:
map(function, sequence[, sequence, ...]) -> list

Return a list of the results of applying the function to the items of
the argument sequence(s).  If more than one sequence is given, the
function is called with an argument list consisting of the corresponding
item of each sequence, substituting None for missing values when not all
sequences have the same length.  If the function is None, return a list of
the items of the sequence (or a list of tuples if more than one sequence).

In [140]: print map(lambda x: x* 2 + 100, foo)
[104, 136, 118, 144, 134, 148, 116, 124, 154]

In [141]: foo
Out[141]: [2, 18, 9, 22, 17, 24, 8, 12, 27]

In [142]: 

 

In [146]: print reduce(lambda x,y: x + y, foo)
139

In [147]: ?reduce
Type:        builtin_function_or_method
String form: <built-in function reduce>
Namespace:   Python builtin
Docstring:
reduce(function, sequence[, initial]) -> value

Apply a function of two arguments cumulatively to the items of a sequence,
from left to right, so as to reduce the sequence to a single value.
For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates
((((1+2)+3)+4)+5).  If initial is present, it is placed before the items
of the sequence in the calculation, and serves as a default when the
sequence is empty.

In [148]: foo
Out[148]: [2, 18, 9, 22, 17, 24, 8, 12, 27]

 

python学习之lambda匿名函数

标签:

原文地址:http://www.cnblogs.com/wenwangt/p/4467277.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!