Python匿名函數

Python匿名函數

@Author:By Runsen

@Date:2019年07月13日

之前寫的,最近決定把之前的回顧,寫詳細。


  • 1.1 lambda 函數
  • 1.2 函數式編程
  • 2.1 map
  • 2.2 filter
  • 2.3 reduce
  • 3.1 如何根據值來排序


1、匿名函數

匿名函數不需要顯示地定義函數名,使用【lambda + 參數 +表達式】的方式

1.1 lambda 函數

lambda 函數的形式

<code>

lambda

argument1, argument2,... argumentN : expression /<code>

套入函數,使用lambda

<code>

square

=

lambda x:

x**2

square(3)

9

/<code>

lambda 返回的一個函數對象

注意:lambda 和def 的區別

lambda 是一個表達式,def 是一個語句

<code>

[(lambda

x:

x*x)(x)

for

x

in

range(10)]

[0,

1

,

4

,

9

,

16

,

25

,

36

,

49

,

64

,

81

]

/<code>

lambda 可以用作函數的參數,def 不能

<code>

l

=

[(1,

20

),

(3,

0

),

(9,

10

),

(2,

-1

)]

l.sort(key=lambda

x:

x[1])

print(l)

[(2,

-1

),

(3,

0

),

(9,

10

),

(1,

20

)]

/<code>

lambda 是隻有一行的簡單表達式

<code>

squared

=

map(lambda

x:

x**2,

[1,

2

,

3

,

4

,

5

])

/<code>

如果不用lambda ,你用def就需要多寫好多行

<code>

def

square

(x)

:

return

x**

2

squared = map(square, [

1

,

2

,

3

,

4

,

5

]) /<code>

在tkinter 中實現的簡單功能

<code>

from

tkinter

import

Button, mainloop button = Button( text=

'This is a button'

, command=

lambda

: print(

'being pressed'

)) button.pack() mainloop() /<code>
Python匿名函數

主要你按壓就出現being pressed

你用def就是下面的樣子

<code>

from

tkinter

import

Button, mainloop

def

print_message

()

:

print(

'being pressed'

) button = Button( text=

'This is a button'

, command=print_message) button.pack() mainloop() /<code>

使用def 要寫好多行,多定義一個函數

1.2 函數式編程

函數式編程是指代碼每一塊都是不可變的,都是由純函數的組成

這裡的純函數 值函數本身相互獨立,對於相同的輸入都有相同的輸出

傳入一個列表將列表的元素變為原來的2倍

<code>

def

multiply_2

(l)

:

for

index

in

range(

0

, len(l)): l[index] *=

2

return

l /<code>

這段代碼不是純函數的形式,因為我多次調用,每次得到的結果不一樣

<code>

def

multiply_2_pure(l):

new_list

=

[]

for

item in l:

* 2)

return

new_list

/<code>

純函數的形式,應該在函數里面定義一個新的列表

2、其他函數

對於純函數python 提供了幾個函數

2.1 map

map 函數的形式

<code>( 

function

,iterable ) /<code>

第一個參數是函數的對象,第二個是一個可迭代對象

<code>

l

=

[1,

2

,

3

,

4

,

5

]

new_list

=

map(lambda

x:

x

*

2

,

l)

list(new_list)

/<code>

2.2 filter

filter通常對一個集合做g過濾的操作

<code>

l

=

[1,

2

,

3

,

4

,

5

]

new_list

=

filter(lambda

x:

x

%

2

==

0

,

l)

list(new_list)

/<code>

2.3 reduce

reduce通常對一個集合做累積的操作

<code>

import

functools

l

=

[1,

2

,

3

,

4

,

5

]

product

=

functools.reduce(lambda

x,

y:

x

*

y,

l)

product

/<code>

3、思考題

3.1 如何根據值來排序

<code>

d

= {

'mike'

:

10

,

'lucy'

:

2

,

'ben'

:

30

} /<code>
<code>

sorted

(d.items(),key=lambda

x

:x[

1

],reverse=True) /<code>

注意 reduce在3中已經放進functools模塊中了

<code> > map(lambda 

x:

x **

2

, [

1

,

2

,

3

,

4

,

5

]) 0x000001CAD42A8CF8> > filter(lambda

x:

x %

2

==

0

, [

1

,

2

,

3

,

4

,

5

]) 0x000001CAD42A8C88> > reduce(lambda x,

y:

x*y,[

1

,

2

,

3

,

4

,

5

]) Traceback (most recent call last): File

""

, line

1

,

in

<

module

>

NameError:

name

'reduce'

is

not

defined

> from functools import reduce > reduce(lambda x,

y:

x*y,[

1

,

2

,

3

,

4

,

5

])

120

map,filter返回的只是一個對象,reduce在

3

中已經放進fucntools模塊中了 /<code>


分享到:


相關文章: