@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
tkinterimport
Button, mainloop button = Button( text='This is a button'
, command=lambda
: print('being pressed'
)) button.pack() mainloop() /<code>
主要你按压就出现being pressed
你用def就是下面的样子
<code>from
tkinterimport
Button, mainloopdef
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
indexin
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=lambdax
:x[1
],reverse=True) /<code>
注意 reduce在3中已经放进functools模块中了
<code> > map(lambdax:
x **2
, [1
,2
,3
,4
,5
]) 0x000001CAD42A8CF8> > filter(lambdax:
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""
, line1
,in
<module
>NameError:
name'reduce'
isnot
defined
> from functools import reduce > reduce(lambda x,y:
x*y,[1
,2
,3
,4
,5
])120
map,filter返回的只是一个对象,reduce在3
中已经放进fucntools模块中了 /<code>