python的map,filter,reduce学习
2,在python3中做了些修改,输出前需要使用list()进行显示转换,而reduce函数则被放到了functools包中
from functools import reduce
import math
def format_name(s):
return s.upper()
def is_odd(x):
return x % 2 == 1
def sqr_integer(x):
r = math.floor(math.sqrt(x))
return x == r*r
def f(x, y):
return x + y
# map 把函数 f 依次作用在 list 的每个元素上,得到一个 iterator 并返回。
print(list(map(format_name, ['adam', 'LISA', 'barT'])))
# reduce()传入的函数 f 必须接收两个参数,reduce()对list的每个元素反复调用函数f,并返回最终结果值。reduce()还可以接收第3个可选参数,作为计算的初始值。
print(reduce(f, [1, 3, 5, 7, 9], 100))
# filter()根据判断结果自动过滤掉不符合条件的元素,返回由符合条件元素组成的iterator。
print(list(filter(is_odd, [1, 4, 6, 7, 9, 12, 17])))
print(list(filter(sqr_integer,range(100))))
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25

运行结果如下
['ADAM', 'LISA', 'BART']
125
[1, 7, 9, 17]
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
- 1
- 2
- 3
- 4
python的map,filter,reduce学习的更多相关文章
- Python中map,filter,reduce,zip的应用
事例1: l=[('main', 'router_115.236.xx.xx', [{'abc': 1}, {'dfg': 1}]), ('main', 'router_183.61.xx.xx', ...
- Python【map、reduce、filter】内置函数使用说明(转载)
转自:http://www.blogjava.net/vagasnail/articles/301140.html?opt=admin 介绍下Python 中 map,reduce,和filter 内 ...
- 【转】Python 中map、reduce、filter函数
转自:http://www.blogjava.net/vagasnail/articles/301140.html?opt=admin 介绍下Python 中 map,reduce,和filter 内 ...
- python常用函数进阶(2)之map,filter,reduce,zip
Basic Python : Map, Filter, Reduce, Zip 1-Map() 1.1 Syntax # fun : a function applying to the iterab ...
- Python【map、reduce、filter】内置函数使用说明
题记 介绍下Python 中 map,reduce,和filter 内置函数的方法 一:map map(...) map(function, sequence[, sequence, ...]) -& ...
- python 内置函数 map filter reduce lambda
map(函数名,可遍历迭代的对象) # 列组元素全加 10 # map(需要做什么的函数,遍历迭代对象)函数 map()遍历序列得到一个列表,列表的序号和个数和原来一样 l = [2,3,4,5,6, ...
- 如何在python3.3用 map filter reduce
在3.3里,如果直接使用map(), filter(), reduce(), 会出现 >>> def f(x): return x % 2 != 0 and x % 3 != 0 ...
- Swift map filter reduce 使用指南
转载:https://useyourloaf.com/blog/swift-guide-to-map-filter-reduce/ Using map, filter or reduce to ope ...
- 数组的高阶方法map filter reduce的使用
数组中常用的高阶方法: foreach map filter reduce some every 在这些方法中都是对数组中每一个元素进行遍历操作,只有foreach是没有 ...
- Python中Lambda, filter, reduce and map 的区别
Lambda, filter, reduce and map Lambda Operator Some like it, others hate it and many are afraid of t ...
随机推荐
- SVN Update Error: Please execute the 'Cleanup' command
尝试用下面两种方法 svn clean up 中有一个选项break lock勾选上 把对应的文件来里的.svn里面的lock文件删除. svn local delete, incoming dele ...
- 【刷题】LOJ 6012 「网络流 24 题」分配问题
题目描述 有 \(n\) 件工作要分配给 \(n\) 个人做.第 \(i\) 个人做第 \(j\) 件工作产生的效益为 \(c_{ij}\) .试设计一个将 \(n\) 件工作分配给 \(n\) ...
- 基于Spark Mllib的文本分类
基于Spark Mllib的文本分类 文本分类是一个典型的机器学习问题,其主要目标是通过对已有语料库文本数据训练得到分类模型,进而对新文本进行类别标签的预测.这在很多领域都有现实的应用场景,如新闻网站 ...
- 【转】为什么 MQTT 是最适合物联网的网络协议
初识 MQTT 为什么 MQTT 是最适合物联网的网络协议 Michael Yuan2017 年 6 月 14 日发布 WeiboGoogle+用电子邮件发送本页面 0 物联网 (IoT) 设备必须连 ...
- 【转】I2C总线协议
I2C总线(Inter Integrated-Circuit)是由PHILIPS公司在上世纪80年代发明的一种电路板级串行总线标准,通过两根信号线——时钟线SCL和数据线SDA——即可完成主从机的单工 ...
- 前端学习 -- Css -- 样式的继承
像儿子可以继承父亲的遗产一样,在CSS中,祖先元素上的样式,也会被他的后代元素所继承, 利用继承,可以将一些基本的样式设置给祖先元素,这样所有的后代元素将会自动继承这些样式. 但是并不是所有的样式都会 ...
- 解题:POJ 2888 Magic Bracelet
题面 这题虽然很老了但是挺好的 仍然套Burnside引理(因为有限制你并不能套Polya定理),思路和这个题一样,问题主要是如何求方案. 思路是把放珠子的方案看成一张图,然后就巧妙的变成了一个经典的 ...
- okhttp在https连接中出现java.net.ProtocolException: Expected ':status' header not present的解决办法
将版本升级到 com.squareup.okhttp3:okhttp:3.9.0可以解决.
- cin,cout,printf,scanf效率对比
From:http://www.cnblogs.com/killerlegend/p/3918452.html Author:KillerLegend Date:2014.8.17 杭电OJ之3233 ...
- 《高性能MySQL》——第一章MySQL的架构与历史
1.可以使用SHOW TABLE STATUS查询表的相关信息. 2.默认存储引擎是InnoDB,如果没有什么很特殊的要求,InnoDB引擎是我们最好的选择. 3.mysql的infobright引擎 ...