【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原生语法中提供了非常多 ...
随机推荐
- ARCGIS 出图显示not map metafile into memory.Not enough memory
有许多的原因,比如系统有问题,磁盘空间不够,或虚拟内存设置不对等.再有就是输出地图时分辨率的设置是否太大等. not enough memory 这个问题是ESRI的一个所谓的“臭名昭著 ...
- Mybatis 系列1-环境搭建
[Mybatis 系列10-结合源码解析mybatis 执行流程] [Mybatis 系列9-强大的动态sql 语句] [Mybatis 系列8-结合源码解析select.resultMap的用法] ...
- 第10课 std::bind和std::function(1)_可调用对象
1. 几种可调用对象(Callable Objects) (1)普通函数指针或类成员的函数指针 (2)具有operator()成员函数的类对象(仿函数).如c++11中的std::function类模 ...
- Install and Configure Apache Kafka
I. Installation The installation environment must have JDK, verify that you enter: java -version 1. ...
- virt-install vs qemu-kvm创建guest主机
virt-install是rpm包python-virtinst里的一个工具 -- 其实就是一个python写的脚本 .基本可以理解为virsh-install是qemu-kvm工具的人性化实现.可以 ...
- @ResponseBody返回中文乱码
1.在方法上修改编码 这种方式,需要对每个方法都进行配置. 2.修改springmvc的配置文件 同时注意,把这个配置写在扫描包的上面.
- is 和 == 区别,id() ,回顾编码,encode(),decode()
1. is 和 == 区别 id()函数 == 判断两边的值 is 判断内存地址例 s = "alex 是 大 xx"# abc = id(s) # 得到内存地址# print(a ...
- JIRA敏捷sprint需求统计设置
1.JIRA->My Dashboard ->添加它的过滤条件 2.过滤条件产生,将sql拷贝至步骤1编辑的JQL中
- Debug版本正常运行,Release版本编译通过但运行崩溃
解决这个问题之前,第一个想的是Debug版本和Release版本有什么区别 Debug版: 经过编译器编译出的项目.exe文件大,而且生成的二进制命令没有经过编译器的优化.项目中包含着丰富的调试信息, ...
- ES6,变量,函数-参数,结构赋值
变量 var 1.可以重复声明. 无法限制修改-, 没有块级作用域 let不能重复声明,变量-可以修改,块级作const不能重复声明,常量-不能修改,块级作 函数——箭头函数function 名字() ...