python reduce & map 习题
基于廖雪峰教程作业
http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/0014317852443934a86aa5bb5ea47fbbd5f35282b331335000
1.利用map()函数,把用户输入的不规范的英文名字,变为首字母大写,其他小写的规范名字。输入:['adam', 'LISA', 'barT'],输出:['Adam', 'Lisa', 'Bart']:
-----答案
# -*- coding: utf-8 -*-
from functools import reduce
def normalize(L):
L=L.lower()
#print(L[0])
L=L[0].upper()+str(L[1:])
#print(L)
return L
#print(normalize("heLLo"))
L1 = ['adam', 'LISA', 'barT']
L2 = list(map(normalize, L1))
print(L2)
-----标准答案
def normalize(L):
return L.capitalize()
习题2:Python提供的sum()函数可以接受一个list并求和,请编写一个prod()函数,可以接受一个list并利用reduce()求积:
def add(a,s):
return a+s
def mult(a,s):
return a*s
def sum(L):
return reduce(add,L)
def prod(L):
return reduce(mult,L)
L=[1,2,3,4]
print(sum(L))
print(prod([3,5,7,9]))
python reduce & map 习题的更多相关文章
- Python【map、reduce、filter】内置函数使用说明(转载)
转自:http://www.blogjava.net/vagasnail/articles/301140.html?opt=admin 介绍下Python 中 map,reduce,和filter 内 ...
- python基础——map/reduce
python基础——map/reduce Python内建了map()和reduce()函数. 如果你读过Google的那篇大名鼎鼎的论文“MapReduce: Simplified Data Pro ...
- python之map、filter、reduce、lambda函数 转
python之map.filter.reduce.lambda函数 转 http://www.cnblogs.com/kaituorensheng/p/5300340.html 阅读目录 map ...
- 【转】Python 中map、reduce、filter函数
转自:http://www.blogjava.net/vagasnail/articles/301140.html?opt=admin 介绍下Python 中 map,reduce,和filter 内 ...
- 使用Python实现Map Reduce程序
使用Python实现Map Reduce程序 起因 想处理一些较大的文件,单机运行效率太低,多线程也达不到要求,最终采用了集群的处理方式. 详细的讨论可以在v2ex上看一下. 步骤 MapReduce ...
- Python【map、reduce、filter】内置函数使用说明
题记 介绍下Python 中 map,reduce,和filter 内置函数的方法 一:map map(...) map(function, sequence[, sequence, ...]) -& ...
- python中map()和dict()的用法
map()用法 map()是python的内置函数,会根据提供的函数对指定序列做映射. 语法: map(func, iter, ...) 其中func为一个功能函数,iter表示可迭代参数序列.map ...
- python基础1习题练习
python基础1习题练习: #encoding:utf-8 #1.实现用户输入用户名和密码,当用户名为 seven 且 密码为 123 时,显示登陆成功,否则登陆失败! name=input('na ...
- python之map
python之Map函数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 # map()函数使用举例 # 功能: ...
随机推荐
- Android获取视频音频的时长的方法
android当中获取视频音频的时长,我列举了三种. 1:获取视频URI后获取cursor cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore ...
- [置顶]
kubernetes-kubectl命令说明
kubectl kubectl controls the K8S cluster manager. Find more information at https://github.com/K8S/K8 ...
- 设计模式之中介者模式(php实现)
github地址:https://github.com/ZQCard/design_pattern /** * 中介者模式(Mediator Pattern)是用来降低多个对象和类之间的通信复杂性. ...
- 【TCP/IP】IP路由选择
IP层在内存中有一个路由表,当有数据要发送时.它要对该表进行一次搜索以确认转发地址.收到的数据到达IP层时,IP层会检查数据报的目的地址是否为本机IP或广播IP: 假设是.就依据IP首部协议字段的协议 ...
- Android服务之bindService源代码分析
上一篇分析startService时没有画出调用ActivityManagerService之前的时序图,这里画出bindService的时序图.它们的调用流程是一致的. 先看ContextWrapp ...
- How is javascript asynchronous AND single threaded?
原文: https://www.sohamkamani.com/blog/2016/03/14/wrapping-your-head-around-async-programming/ ------- ...
- 系统封装 EasyBoot如何将WIN7安装版提取到光盘
1 将WIN7光盘中的文件提取到Easyboot根目录,注意不要autorun.inf和setup.exe这两个文件.我们这里的Easyboot已经有了一些其他东西(XP的安装版文件,PE的文件等等, ...
- Linux非阻塞IO(七)使用epoll重新实现客户端
使用poll与epoll的区别主要在于: poll可以每次重新装填fd数组,但是epoll的fd是一开始就加入了,不可能每次都重新加入 于是采用这种策略: epoll除了listenfd一开始就监听r ...
- 基于React的PC网站前端架构分析
代码地址如下:http://www.demodashi.com/demo/12252.html 本文适合对象 有过一定开发经验的初级前端工程师: 有过完整项目的开发经验,不论大小: 对node有所了解 ...
- 我是怎样理解web页面的
事实上web页面包括三部分东东 1.页面展示的元素(HTML) 2.页面元素展示的样式(CSS) 3.控制页面元素的交互(JavaScript) 不管页面多么复杂,从这三方面去看,都会得到清晰的认识的 ...