您的位置:首页 > 其它

L21匿名函數lambda()

2016-03-18 15:52 351 查看
lambda(參數:返回值)

>>> def ds(x):
return 2*x+1

>>> ds(5)
11
>>> lambda x : 2*x+5
<function <lambda> at 0x02C66D68>
>>> g = lambda x : 2*x+5
>>> g(5)
15

#兩個參數
>>> def add(x,y):
return x + y

>>> add(3,4)
7
>>>
>>> lambda x,y : x + y
<function <lambda> at 0x02EEA078>
>>> g = lambda x,y : x + y
>>> g(3 , 7)
10

#filter(function, iterable)
#過濾器:把任何非true的內容過濾掉

>>> filter(None,[1, 0, False, True])
<filter object at 0x02C8D910>
>>> list(filter(None,[1, 0, False, True]))
[1, True]

---------------------------------------------------------------

>>> def odd(x):
x = x%2
return x

>>> temp = range(10)

>>> show = filter(odd,temp)

>>> list(show)
[1, 3, 5, 7, 9]

>>> list(filter(lambda x:x%2, range(10)))
[1, 3, 5, 7, 9]                          

#因為偶數的話餘0>>>False

-----------------------------------------------------------------
#map()映射:將序列的函數作為每一個函數加工

>>> list(map(lambda x:x*2, range(10)))
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: