11月06日(第6天)
来源:互联网 发布:风云防火墙 网络规则 编辑:程序博客网 时间:2024/06/01 12:08
Morning
主要介绍了lambda函数、map函数、reduce函数和filter函数
1. 用reduce函数求一个列表的乘积。
L2 = [2,5,7,10,21]from functools import reducereduce(lambda x,y:x*y, L2)
运行结果:
14700
2. 用ruduce思想找一个列表中的最大值。
L2 = [34,58,17,10,21]from functools import reducedef mymax(x,y): if x>=y: a = x else: a = y return areduce(mymax, L2)
运行结果:
58
代码参考悦小妹的代码进行简化:
L2 = [34,58,17,10,21]from functools import reducedef mymax(x,y): if x>=y: return x else: return y reduce(mymax, L2)
3. 给出一个列表,只想对其中的奇数运算,之后算出每个奇数的平方,再把所有的平方求和(要求用到map、reduce、filter函数)。
#Step1:筛选出奇数L3 = [1,2,4,5,8,12,19,27]def is_odd(n): return n % 2 == 1L3i=list(filter(is_odd,L3))print(L3i)#Step2:算出每个奇数的平方L3ii = list(map(lambda x:x**2, L3i))print(L3ii)#Step3:把所有的平方求和from functools import reducereduce(lambda x,y:x+y,L3ii)
运行结果:
[1, 5, 19, 27]
[1, 25, 361, 729]
Out[30]: 1116
阅读全文
0 0
- 11月06日(第6天)
- 11月26日——培训第6天
- 暑假第6天 7月11日
- 11月23日——培训第3天
- 11月24日——培训第4天
- 11月25日——培训第5天
- 11月27日——培训第7天
- 11月28日——培训第8天
- 11月30日——培训第9天
- 12月2日——培训第11天
- 12月11日——培训第19天
- 1月11日——培训第42天
- 3月11日——培训第77天
- 2015年07月18日第11天笔记
- 1月6日——培训第38天
- 2月6日——培训第65天
- 3月6日——培训第73天
- 贵州支教之第五天(11月11日)
- 《剑指offer》刷题笔记(举例让抽象具体化):二叉搜索树的后序遍历序列
- 01--js判断变量为null或者""
- 跨域问题解决方案(HttpClient安全跨域 & jsonp跨域)
- 来自大学生投稿:一个适合新手上手的商城项目
- Java注解的实现原理
- 11月06日(第6天)
- angular4 环境变量配置
- log4j2配置文件详解
- IN 查询的时候索引不起作用?
- 欢迎使用CSDN-markdown编辑器
- Symmetric Tree
- 软件源码破解工具De4Dot用法,net破解、反混淆
- 11月赠书:Android、Kotlin、Gradle的书都有
- 随笔五、功能测试