Python: map() and reduce()
map()函数接收两个参数,一个是函数,一个是序列,map将传入的函数依次作用到序列的每个元素,并把结果作为新的list返回。
举例说明,比如我们有一个函数f(x)=x2,要把这个函数作用在一个list [1, 2, 3, 4, 5, 6, 7, 8, 9]上,就可以用map()实现如下:
def f(x):
return x*x
map(f,[1,2,3,4,5,6,7,8,9]
result:
[1, 4, 9, 16, 25, 36, 49, 64, 81]
reduce把一个函数作用在一个序列[x1, x2, x3...]上,这个函数必须接收两个参数,reduce把结果继续和序列的下一个元素做累积计算,其效果就是:
reduce(f, [x1, x2, x3, x4]) = f(f(f(x1, x2), x3), x4)
Practice:
【练习1】利用map()函数,把用户输入的不规范的英文名字,变为首字母大写,其他小写的规范名字。输入:['adam', 'LISA', 'barT'],输出:['Adam', 'Lisa', 'Bart']。
答:
def firstToUpper(x):
s1 = x[:1].upper()
s2 = x[1:].lower()
s3 = s1 + s2
return s3
map(firstToUpper,['aleN','TOM','hello'])
解释: x[:1]: x string 的第一个字符(从 0 开始到 1 下标但是不包含1下标); x[1:]: x string 的第二个字符一直到最后, 即从下标 1 开始到最后。。。
结果:
['Alen', 'Tom', 'Hello']
【练习2】Python提供的sum()函数可以接受一个list并求和,请编写一个prod()函数,可以接受一个list并利用reduce()求积。
def prod(x,y):
return x+y
reduce(prod,[1,2,3,4,5,6,7,8100])
结果:
8128
Python: map() and reduce()的更多相关文章
- Python map,filter,reduce函数
# -*- coding:utf-8 -*- #定义一个自己的map函数list_list = [1,2,4,8,16] def my_map(func,iterable): my_list = [] ...
- python Map()和reduce()函数
Map()和reduce()函数 map() 会根据提供的函数对指定序列做映射. 第一个参数 function 以参数序列中的每一个元素调用 function 函数,返回包含每次 function 函 ...
- python map filter reduce的优化使用
这篇讲下python中map.filter.reduce三个内置函数的使用方式,以及优化方法. map()函数 map()函数会根据提供的函数对指定序列做映射. 语法: map(function,it ...
- python map函数 reduce函数
Python中map()函数浅析 函数式编程: 更好的描述问题 map函数 怎么理解当传入多个参数list时,map如何运作: abc函数第一次传入的数据时 (11,44,77),然后(22,5 ...
- 计算机基础,Python - Map和Reduce
例子1. python中实现hashable def __hash__(self): hashes = map(hash, self.components) return functools.redu ...
- Python: map和reduce
可以先google一篇论文:MapReduce: SImplified Data Processing on Large Clusters 1. map map()函数接收2个参数:一个是函数,一个是 ...
- Python map filter reduce enumerate zip 的用法
map map(func, list) 把list中的数字,一个一个运用到func中,常和lambda一起用. nums = [1, 2, 3, 4, 5] [*map(lambda x: x**2, ...
- python map() filter() reduce()函数的用法以及实例
map() 看一下我的终端咋说: map()的函数用法: map(function, iterable, ...) 看一下具体例子: 注意的是一定要强制转化一下才能输出 也可以写匿名函数: (mark ...
- 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的request包
1,request包基本用法 import requests a=requests.get("http://www.baidu.com")print a.text #以文本形式打印 ...
- 【Java编程思想笔记】注解--自定义注解
文章参考自:https://www.cnblogs.com/xdp-gacl/p/3622275.html 学习网站:how2java.cn 一.自定义注解的创建过程 第一步:(元注解) 使用元注 ...
- Chrome 远程代码执行漏洞CVE-2019-5786-EXP
0x01 漏洞原理 CVE-2019-5786是位于FileReader中的UAF漏洞,由Google's Threat Analysis Group的Clement Lecigne于2019-02- ...
- Java开发之@PostConstruct和@PreDestroy注解
从Java EE5规范开始,Servlet增加了两个影响Servlet生命周期的注解(Annotation):@PostConstruct和@PreConstruct.这两个注解被用来修饰一个非静态的 ...
- formData 对象 与 Content-Type 类型
FormData FormData对象用以将数据编译成键值对,以便用XMLHttpRequest来发送数据.其主要用于发送表单数据,但亦可用于发送带键数据(keyed data),而独立于表单使用.如 ...
- 【算法】【python实现】二叉搜索树插入、删除、查找
二叉搜索树 定义:如果一颗二叉树的每个节点对应一个关键码值,且关键码值的组织是有顺序的,例如左子节点值小于父节点值,父节点值小于右子节点值,则这棵二叉树是一棵二叉搜索树. 类(TreeNode):定义 ...
- bat路径中有空格
例如bat文件中写 C:/Program Files (x86)/Google/Chrome/Application/chrome.exe ./html/index.html pause 会报错, ...
- C++中STL常用容器的优点和缺点
我们常用到的STL容器有vector.list.deque.map.multimap.set和multiset,它们究竟有何区别,各自的优缺点是什么,为了更好的扬长避短,提高程序性能,在使用之前需要我 ...
- scrapy 通过FormRequest模拟登录再继续
1.参考 https://doc.scrapy.org/en/latest/topics/spiders.html#scrapy.spiders.Spider.start_requests 自动提交 ...
- excel数据导入mysql
先把excel数据另存成txt文件 Load Data InFile 'D:/1.txt' Into Table `res_type_collect` fields terminated by '@‘ ...