参考:4. Map, Filter and Reduce — Python Tips 0.1 documentation

参考:Python的functools.reduce用法


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筛选 的用法的更多相关文章

  1. Python一个有意思的地方:reduce、map、filter

    今天阅读了关于Python函数式编程的系列文章,地址在这里: http://www.cnblogs.com/huxi/archive/2011/06/24/2089358.html 里面提到了四个内建 ...

  2. java关于map用来筛选的用法

    我有一个实体 PropTemplateItem{id,名称,父节点,模版id},父节点为root是定义为根节点. 例如数据: 001,颜色,root,123 002,白色,001,123 003,红色 ...

  3. lambda匿名函数,sorted排序,filter()筛选,map()映射

    一丶匿名函数 语法: 函数名 = lambda参数:返回值 # 普通的正常的函数 def func(n): return n * n ret = func(9) print(ret) # 匿名函数 a ...

  4. 闭包 -> map / floatMap / filter / reduce 浅析

    原创: 转载请注明出处 闭包是自包含的函数代码块,可以在代码中被传递和使用 闭包可以捕获和存储其所在上下文中任意常量和变量的引用.这就是所谓的闭合并包裹着这些常量和变量,俗称闭包.Swift 会为您管 ...

  5. map、filter、reduce、lambda

    一.map.filter.reduce map(fuction , iterable) 映射 对可迭代对象中的每一项,使用函数去改变 filter(function, iterable) 过滤 可迭代 ...

  6. Python 函数之lambda、map、filter和reduce

    1.lambda函数 lambda()是Python里的匿名函数,其语法如下: lambda [arg1[, arg2, ... argN]]: expression 学习条件运算时,对于简单的 if ...

  7. Swift函数编程之Map、Filter、Reduce

    在Swift语言中使用Map.Filter.Reduce对Array.Dictionary等集合类型(collection type)进行操作可能对一部分人来说还不是那么的习惯.对于没有接触过函数式编 ...

  8. python之map、filter、reduce、lambda函数 转

    python之map.filter.reduce.lambda函数  转  http://www.cnblogs.com/kaituorensheng/p/5300340.html 阅读目录 map ...

  9. ES6 数组遍历方法的实战用法总结(forEach,every,some,map,filter,reduce,reduceRight,indexOf,lastIndexOf)

    目录 forEach every some map filter reduce && reduceRight indexOf lastIndexOf 前言 ES6原生语法中提供了非常多 ...

随机推荐

  1. java中synchronized 用在实例方法和对象方法上面的区别

    https://bijian1013.iteye.com/blog/1836575 在Java中,synchronized 是用来表示同步的,我们可以synchronized 来修饰一个方法.也可以s ...

  2. ArcMap VBA实现连续编号

    连续编号VBA部分:Static lCount as longlCount=lCount+1赋值部分:lCount (从1开始)lCount-1 (从0开始)

  3. 第2章 GNS3和PacketTracer网络模拟器(3)_搭建Packet tracer实验环境

    3. Packet tracer实验环境 3.1 设置网络拓扑图 (1)配置路由器局域网和广域网接口,如上图(可双击相应的图标,然后在命令行或图形界面上进行IP地址等配置) ①本例采用“Generic ...

  4. servlet为什么会出现?servlet有什么作用?

    在面试中遇到了这个问题,所以想总结一下,所以上网找了一下,主要是网上的一些我觉得比较合理的解释: servlet为什么会出现? Servlet = Service + Applet,表示小服务程序.S ...

  5. C语言格式化%整理

    以输出为例: #include <stdio.h> main() { printf("**进制****************************************** ...

  6. Ubuntu17.04下安装vmware虚拟机

    linux常用虚拟机一般为KVM,Vmware或者VirtualBox(简称VBox). 下面给大家介绍以下如何在ubuntu17.04版本上安装vmware虚拟机至于虚拟机是用来干啥的,在这里我就不 ...

  7. MySQL表中的数据类型

    数据类型:在表中数据类型主要是限制字段必须以什么样的数据类型传值. 一 整型 整数类型:TINYINT SMALLINT MEDIUMINT INT BIGINT总共有五种,name我们一般用到的也就 ...

  8. 微信小程序连接本地接口(转)

    原文地址 最近的一个项目就是微信小程序 第一次接触微信开发者工具,并进行小程序的后端开发, 于是想看一下小程序如何请求本地的后台服务接口 wx.request({ url: 'http://local ...

  9. 【接口测试】【SOAP】简单的接口测试学习

    ==================================================================================================== ...

  10. python-html-百度云音视频点播服务

    加密的视频: 一:python后端需要生成token token计算规则 名词解释: userId: 百度云用户唯一标识,可在百度云管理控制台账号基本信息中得到,32位字符串. userKey:用户密 ...