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()的更多相关文章

  1. Python map,filter,reduce函数

    # -*- coding:utf-8 -*- #定义一个自己的map函数list_list = [1,2,4,8,16] def my_map(func,iterable): my_list = [] ...

  2. python Map()和reduce()函数

    Map()和reduce()函数 map() 会根据提供的函数对指定序列做映射. 第一个参数 function 以参数序列中的每一个元素调用 function 函数,返回包含每次 function 函 ...

  3. python map filter reduce的优化使用

    这篇讲下python中map.filter.reduce三个内置函数的使用方式,以及优化方法. map()函数 map()函数会根据提供的函数对指定序列做映射. 语法: map(function,it ...

  4. python map函数 reduce函数

    Python中map()函数浅析 函数式编程: 更好的描述问题 map函数 怎么理解当传入多个参数list时,map如何运作:    abc函数第一次传入的数据时 (11,44,77),然后(22,5 ...

  5. 计算机基础,Python - Map和Reduce

    例子1. python中实现hashable def __hash__(self): hashes = map(hash, self.components) return functools.redu ...

  6. Python: map和reduce

    可以先google一篇论文:MapReduce: SImplified Data Processing on Large Clusters 1. map map()函数接收2个参数:一个是函数,一个是 ...

  7. Python map filter reduce enumerate zip 的用法

    map map(func, list) 把list中的数字,一个一个运用到func中,常和lambda一起用. nums = [1, 2, 3, 4, 5] [*map(lambda x: x**2, ...

  8. python map() filter() reduce()函数的用法以及实例

    map() 看一下我的终端咋说: map()的函数用法: map(function, iterable, ...) 看一下具体例子: 注意的是一定要强制转化一下才能输出 也可以写匿名函数: (mark ...

  9. python常用函数进阶(2)之map,filter,reduce,zip

    Basic Python : Map, Filter, Reduce, Zip 1-Map() 1.1 Syntax # fun : a function applying to the iterab ...

随机推荐

  1. 解压unzip的用法

    1.把文件解压到当前目录下 [root@instance-q6z0ksfb xmerge_test]# unzip db1.zip 2.把文件解压到指定的目录下,需要用到-d参数. unzip -d ...

  2. 「luogu2680」[NOIp2015] 运输计划

    题目大意:给定一棵n个节点的树,输入m组一条链的两个端点:把树上的某个边权改为0,求m条链长度的最大值的最小值: 一.考虑二分: 1.对于需要判断是否为可行方案的 mid,所有链长不大于 mid 的链 ...

  3. zookeep服务启动命令

    zookeeper的安装目录:/usr/local/zookeeper-3.4.6/bin/zkServer.sh; 配置文件路径:../conf/zoo.cfg 端口 :2181: ZooKeepe ...

  4. Zombie Scanning

    1.theree -way handshake A TCP SYN packet is sent from the device that wishes to establish a connecti ...

  5. [原创]FPGA JTAG工具设计(一)

    先来看不同JTAG方案,下载配置QSPI Flash所耗时间 基于FTDI方案,JTAG下载时间为494sec JTAG chain configuration ------------------- ...

  6. Codeforces 522D Closest Equals

    题解: 傻逼题 直接从左向右扫描每个点作为右端点 然后单点修改区间查询就行了 另外一种更直观的做法就是$(i,j)$之间产生了$(j-i)$ 于是变成矩形查最大值,kd-tree维护 代码: #inc ...

  7. ajax 函数回调

    var initTaxPriod = function (taxNo) { intitSearch(); $("#taxPeriod").html(""); t ...

  8. js隐藏元素、jquery隐藏、css隐藏

    $("#yh").css("display","none");//隐藏元素 $("#yh").css("dis ...

  9. idea报错:Invalid bound statement (not found)

    在配置MyBatis接口映射的Mapper.xml时,提示Invalid bound statement (not found)异常,就算是接口和xml名字相同,路径相同也无法找到,在网上找到了几种解 ...

  10. Mapreduce的序列化和流量统计程序开发

    一.Hadoop数据序列化的数据类型 Java数据类型 => Hadoop数据类型 int IntWritable float FloatWritable long LongWritable d ...