【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原生语法中提供了非常多 ...
随机推荐
- Map相关问题
<!--加载地图开始--> <!DOCTYPE html><html><head> <meta charset="UTF-8" ...
- go语言功能代码
一.数据类型转换 package main import ( "fmt" "strconv" ) func main() { //int到string str ...
- MongoDB集群搭建之主从模式
单机搭建 #创建docker持久化数据目录 [root@docker ~]# mkdir -p /root/application/program/mongodb/data/master-slaveM ...
- Redis进阶实践之十四 Redis-cli命令行工具使用详解
转载来源:http://www.cnblogs.com/PatrickLiu/p/8508975.html 一.介绍 redis学了有一段时间了,以前都是看视频,看教程,很少看官方的东西.现在redi ...
- 采用boosting思想开发一个解决二分类样本不平衡的多估计器模型
# -*- coding: utf-8 -*- """ Created on Wed Oct 31 20:59:39 2018 脚本描述:采用boosting思想开发一个 ...
- 关于Vue实例的生命周期created和mounted的区别
什么是生命周期 Vue实例有一个完整的生命周期,也就是从开始创建.初始化数据.编译模板.挂载Dom.渲染→更新→渲染.卸载等一系列过程,我们称这是Vue的生命周期.通俗说就是Vue实例从创建到销毁的过 ...
- C++删除容器数据
// free the contents of the list; erase the list inline void ListDelete (list <void *> *pList) ...
- (转)C# WebApi 异常处理解决方案
原文地址:http://www.cnblogs.com/landeanfen/p/5363846.html 一.使用异常筛选器捕获所有异常 我们知道,一般情况下,WebApi作为服务使用,每次客户端发 ...
- C#提取双引号中的字符串
public static void Main(string[] args) { string strtmp = "123\"456\"qqq\"789\&qu ...
- uWSGI, Gunicorn, 啥玩意儿?
因为nginx等优秀的开源项目,有不少本来不是做服务器的同学也可以写很多服务器端的程序了.但是在聊天中会发现,大家虽然写了不少代码,但是对wsgi是什么,gunicorn是什么,反向代理又是什么并不了 ...