Python内置函数filter, map, reduce
filter、map、reduce,都是对一个集合进行处理,filter很容易理解用于过滤,map用于映射,reduce用于归并. 是Python列表方法的三架马车.
1. filter函数的功能相当于过滤器。
print range(1,6) # 1,2,3,4,5
print reduce(lambda x,y:x*y, range(1,6)) #1*2*3*4*5
def action(x):
return lambda y: x+y
f = action(2) # this is a lambda object
print f(3) # 5
----------------------------------------------------------------------------------
>>>
print
reduce
(
lambda
x,y:x
*
y,
range
(
1
,
6
)) 120
>>>
print
reduce
(
lambda
x,y:x
*
y,
range
(
1
,
5
)) 24
>>>
print
reduce
(
lambda
x,y:x
*
y,
range
(
1
,
4
)) 6
>>>
print
reduce
(
lambda
x,y:x
*
y,
range
(
1
,
3
)) 2
>>>
print
reduce
(
lambda
x,y:x
*
y,
range
(
1
,
2
)) 1
>>>
print
map
(
lambda
a:
reduce
(
lambda
x,y:x
*
y,
range
(
1
,a
+
1
)),
range
(
1
,
6
))
[
1
,
2
,
6
,
24
,
120
]
>>>
print
reduce
(
lambda
m,n:m
+
n,
map
(
lambda
a:
reduce
(
lambda
x,y:x
*
y,
range
(
1
,a
+
1
)),
range
(
1
,
6
)))
153
质数又称素数。指在一个大于1的自然数中,除了1和此整数自身外,不能被其他自然数整除的数
>>>
filter
(
lambda
N:
len
(
filter
(
lambda
M:N
%
M
=
=
0
,
range
(
2
,
int
(N
*
*
0.5
)
+
1
)))
=
=
0
,
range
(
100
,
201
))
Python内置函数filter, map, reduce相关推荐
- 自定义生成器函数模拟Python内置函数filter()
祝"Python小屋"全体朋友新年快乐,身体健康,万事如意! ================== 作为Python函数式编程的三大巨头之一,内置函数filter()的地位是非常重 ...
- Python内置函数filter()和匿名函数lambda解析
一.内置函数filter filter()函数是 Python 内置的一个高阶函数,filter()函数接收一个函数 f 和一个list,这个函数 f 的作用是对每个元素进行判断,返回由符合条件迭代器 ...
- python内置函数之map函数的水桶效应------计算几个列表中相同位置的数据的和
与内置函数zip()都是水桶效应 lst1 = [1, 3, 5, 7] lst2 = [2, 4, 6, 8, 10] m = map(lambda x, y, z: x + y+ z, lst1, ...
- Python内置函数zip map filter的使用
并行遍历zip zip会取得一个或多个序理为参数,然后返回元组的列表,将这些序列中的并排的元素配成对. L1=[1,2,3,4] L2=[5,6,7,8] L3=zip(L1,L2) print(L3 ...
- Python 内置函数 filter()
假设出现异常收到一个10万条类脏数据,类似底下: information = [158804023652,'evan','互联网行业','阿里巴巴',15625445635,15884236525.. ...
- [转载] Python 内置函数 lambda、filter、map、reduce
参考链接: Python lambda (匿名函数) | filter, map, reduce Python 内置了一些比较特殊且实用的函数,使用这些能使你的代码简洁而易读. 下面对 Python ...
- python filter内置函数_python 内置函数filter
python 内置函数filter class filter(object): """ filter(function or None, iterable) --> ...
- python filter函数_python 内置函数filter
python 内置函数filter class filter(object): """ filter(function or None, iterable) --> ...
- python内置函数boolsum_Python - 内置函数
Python - 内置函数 filter filter(处理逻辑, 可遍历对象)遍历序列中的每个元素,判断每个元素的布尔值为True则保留元素. 用法:返回执行结果为TRUE的入参(入参是列表字符元组 ...
最新文章
- 神经网络中的神经元常见激活函数绘制
- 欢迎参加“城市大脑与应急管理”专家研讨会
- Android之自定义控件显示点赞用户并通过用户名称进入该用户主页的功能
- 服务器如何删除所有磁盘信息,关于怎样删除EFI分区,简单省事,很多换下来的硬盘都有这个分区...
- left join 重复数据_Python数据分析整理小节
- 自动注入、加载 properties 文件、scope 属性、单例设计模式
- 男朋友花3000元买一块电脑显卡,他是怎么想的?
- Javascript Number.MIN_VALUE
- c语言程序设计教程期末考试,《C语言程序设计》课程考核方案
- Linux学习之Linux系统目录简概
- ThinkPhp报错:thinkphp\library\think\Template.php Line(1243) template not exists:...test\...\index.html
- Alpha通道的概念与功能
- Code Review之P3C的安装使用
- Comsol 2020全套教学视频 教程入门讲解新手的福音
- 相比于深度学习,传统的机器学习算法难道就此没落了吗,还有必要去学习吗?
- 迅为IMX6ULL教程更新至2060+页,裸机开发,Linux系统移植,驱动开发,构建文明系统,QT开发,系统编程
- 迭代最小二乘拟合椭圆
- CentOS7安装nfdump+nfsen+sflow 实现流量监控
- cf 1324D. Pair of Topics
- Numpy + Matplotlib 绘制白色画图