【384】reduce归纳、map映射、filter筛选 的用法
参考:4. Map, Filter and Reduce — Python Tips 0.1 documentation
Map:映射,对于列表的每个元素进行相同的操作
filter:筛选,筛选列表中满足某一条件的所有元素
reduce:归纳,连续操作,连加、连乘等
python 3.0以后, reduce已经不在built-in function里了, 要用它就得from functools import reduce.
reduce的用法
reduce(function, sequence[, initial]) -> value
Apply a function of two arguments cumulatively to the items of a sequence,
from left to right, so as to reduce the sequence to a single value.
For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates
((((1+2)+3)+4)+5). If initial is present, it is placed before the items
of the sequence in the calculation, and serves as a default when the
sequence is empty.
意思就是对sequence连续使用function,
如果不给出initial, 则第一次调用传递sequence的两个元素,
以后把前一次调用的结果和sequence的下一个元素传递给function. 如果给出initial,
则第一次传递initial和sequence的第一个元素给function.
from functools import reduce
reduce(lambda x,y: x+y, [1, 2, 3])
reduce(lambda x,y: x+y, [1, 2, 3], 9) reduce(lambda x,y: x*y, [1, 2, 3, 4])
reduce(lambda x,y: x*y, [1, 2, 3, 4], 5) reduce(lambda x,y: x**y, [2, 3, 4]) output:
6
15
24
120
4096
Example from Ed of COMP9021
question:
For instance, dict1 = {'Lucy' : 'I am a Knight', 'Laser':'I am a Knaves'}
list1 = [(0,0), (0,1), (1,0),(1,1)]
how do I put the output like this:
{'Lucy' : 0, 'Laser':0}
{'Lucy' : 0, 'Laser':1}
{'Lucy' : 1, 'Laser':0}
{'Lucy' : 1, 'Laser':1}
answers:
dict1 = {'Lucy' : 'I am a Knight', 'Laser':'I am a Knaves'}
list1 = [(0,0), (0,1), (1,0),(1,1)]
answer = [i for i in map(lambda x:{[key for key in dict1][0]:x[0], [key for key in dict1][1]:x[1]}, list1)]
for i in answer:
print(i)
【384】reduce归纳、map映射、filter筛选 的用法的更多相关文章
- Python一个有意思的地方:reduce、map、filter
今天阅读了关于Python函数式编程的系列文章,地址在这里: http://www.cnblogs.com/huxi/archive/2011/06/24/2089358.html 里面提到了四个内建 ...
- java关于map用来筛选的用法
我有一个实体 PropTemplateItem{id,名称,父节点,模版id},父节点为root是定义为根节点. 例如数据: 001,颜色,root,123 002,白色,001,123 003,红色 ...
- lambda匿名函数,sorted排序,filter()筛选,map()映射
一丶匿名函数 语法: 函数名 = lambda参数:返回值 # 普通的正常的函数 def func(n): return n * n ret = func(9) print(ret) # 匿名函数 a ...
- 闭包 -> map / floatMap / filter / reduce 浅析
原创: 转载请注明出处 闭包是自包含的函数代码块,可以在代码中被传递和使用 闭包可以捕获和存储其所在上下文中任意常量和变量的引用.这就是所谓的闭合并包裹着这些常量和变量,俗称闭包.Swift 会为您管 ...
- map、filter、reduce、lambda
一.map.filter.reduce map(fuction , iterable) 映射 对可迭代对象中的每一项,使用函数去改变 filter(function, iterable) 过滤 可迭代 ...
- Python 函数之lambda、map、filter和reduce
1.lambda函数 lambda()是Python里的匿名函数,其语法如下: lambda [arg1[, arg2, ... argN]]: expression 学习条件运算时,对于简单的 if ...
- Swift函数编程之Map、Filter、Reduce
在Swift语言中使用Map.Filter.Reduce对Array.Dictionary等集合类型(collection type)进行操作可能对一部分人来说还不是那么的习惯.对于没有接触过函数式编 ...
- python之map、filter、reduce、lambda函数 转
python之map.filter.reduce.lambda函数 转 http://www.cnblogs.com/kaituorensheng/p/5300340.html 阅读目录 map ...
- ES6 数组遍历方法的实战用法总结(forEach,every,some,map,filter,reduce,reduceRight,indexOf,lastIndexOf)
目录 forEach every some map filter reduce && reduceRight indexOf lastIndexOf 前言 ES6原生语法中提供了非常多 ...
随机推荐
- ha环境下重新格式化hdfs报错
datanode启动不成功,如下所示,我的136,137.138都是datanode,都启动不了. 查看datanode日志文件发现报错: 一个报错Incompatible clusterIDs in ...
- 让android程序根据重力感应旋转屏幕(支持4个方向旋转)
原文地址:http://blog.csdn.net/yixiaoqingyuz/article/details/6453798代码如下: ChangeOrientationHandler.java p ...
- echarts饼图配置
js引用和配置div <div id="container" style="height: 100%"></div> <scrip ...
- (转)开源OpenWRT知识
原博地址:http://www.thinkingquest.net/article/466 我们都需要使用google提供的搜索,gmail等优质服务.但是由于方墙的存在,使得大家各自搞各自的FQ办法 ...
- 通过 SSH 转发TCP连接数据
设定 首先双方的/etc/ssh/sshd_config设定以下四项: AllowAgentForwarding yes AllowTcpForwarding yes GatewayPorts yes ...
- 快速部署MySQL数据库
一.下载对应的软件版本 下载地址:http://mirrors.sohu.com/mysql/MySQL-5.6/ [root@localhost ~]# wget -q http://mirrors ...
- TextView右上角显示小红点,小红点根据TextView的长度移动,小红点被TextView挤出去不显示的问题;
大概就是图片这个样,这个功能很常见,本来我以为很简单,谁知道真的很简单: 遇到点小问题,记录一下,哈哈: 小红点的Drawable: <?xml version="1.0" ...
- 常用正则表达式—邮箱(Email)
本文针对有一点正则基础的同学,如果你对正则一无所知,请移步“正则表达式30分钟入门教程”学习. 要验证一个字符串是否为邮箱的话,首先要了解邮箱账号的格式.我尝试过在网上找出一个标准的格式,但是很遗憾 ...
- oracle常用加解密函数
md5 CREATE OR REPLACE FUNCTION MD5( passwd IN VARCHAR2) RETURN VARCHAR2 IS retval varchar2(32); BEGI ...
- TableStore:创建SyncClient+getRow读取一行数据
1.通过控制台或者客户端,在TableStore中新建了实例owlforest,在实例详情中获取到实例访问地址endPoint 2.新建表user,确定主键为userid(Interger)类型,因为 ...