Python -- lambda, map, filter
lambda
f = lambda x : x * 2 f(5) f = lambda x,y,z : x+y+z f(2,1,3)
map
list(map(lambda x:x[0].upper()+x[1:].lower(), ['sQd', 'ZORO'])) #传入列表,首字母变大写,其余变小写
reduce
from functools import reduce def add(x, y):
return x + y reduce(add, [1,2,3,4])
reduce(f, [x1, x2, x3, x4]) = f(f(f(x1, x2), x3), x4)
filter
list(filter(lambda n: n%2 == 1, [1,2,3,4,5])) #保留奇数,舍弃偶数
list(filter(lambda s: s and s.strip(), ['S', '', None, 'b'])) #删除一个列表中的空元素
Python -- lambda, map, filter的更多相关文章
- [译]PYTHON FUNCTIONS - MAP, FILTER, AND REDUCE
map, filter, and reduce Python提供了几个函数,使得能够进行函数式编程.这些函数都拥有方便的特性,他们可以能够很方便的用python编写. 函数式编程都是关于表达式的.我们 ...
- python中 Lambda,Map,Filter,Itertools,Generator高级函数的用法
Lambda 函数 Lambda 函数是一种比较小的匿名函数--匿名是指它实际上没有函数名. Python 函数通常使用 def a_function_name() 样式来定义,但对于 lambda ...
- [Python学习笔记-002] lambda, map, filter and reduce
1. lambda lambda, 即匿名函数,可以理解为跟C语言的宏类似.例如: >>> max = lambda x, y: x if x > y else y >& ...
- Python面试题之Python中的lambda map filter reduce zip
当年龟叔想把上面列出来的这些都干掉.在 “All Things Pythonic: The fate of reduce() in Python 3000”这篇文章中,他给出了自己要移除lambda. ...
- Python: lambda, map, reduce, filter
在学习python的过程中,lambda的语法时常会使人感到困惑,lambda是什么,为什么要使用lambda,是不是必须使用lambda? 下面就上面的问题进行一下解答. 1.lambda是什么? ...
- python中的内置函数lambda map filter reduce
p.p1 { margin: 0; font: 12px "Helvetica Neue" } p.p2 { margin: 0; font: 12px "Helveti ...
- python库函数Map, Filter and Reduce的用法
python中有三个函数式编程极大的简化了程序的复杂性,这里就做一下讨论和记录. 一 Map:应用在链表输入所有元素的函数,它的格式如下所示: map(function_to_apply, list_ ...
- lambda(),map(),filter()
Lambda 函数 Lambda 函数是一种比较小的匿名函数.Python 函数通常使用 def a_function_name() 样式来定义,但对于 lambda 函数,我们根本没为它命名.这是因 ...
- Python中map,filter,reduce,zip的应用
事例1: l=[('main', 'router_115.236.xx.xx', [{'abc': 1}, {'dfg': 1}]), ('main', 'router_183.61.xx.xx', ...
随机推荐
- 【笔记】Loadrunner添加OS类型为Windows的服务器(Win7)
最近在学习Loadrunner,看到“监控Windows资源”,决定小试一把,由于没有找到合适的镜像,暂时没有搞好Windows的虚拟机,so 先用自己小试牛刀了只有,不过这样子好像难度锐减也~只要小 ...
- java innerclass
---恢复内容开始--- 内部类: public class Inner{ public class Inner2{} } 创建内部类对象 .new public class Test { in ...
- SQL Server服务开闭
SQL Server(MSSQLSERVER)是必须要开启的,这个是数据库引擎服务,就像汽车的发动机一样. SQL Server代理(MSSQLSERVER)是代理服务,比如你有一些自动运行的,定时作 ...
- yali项目的slider
// 调用 var s41 = new slider({ target : '#slider411', titleActiveClass : 'j-active', itemActiveClass : ...
- SpringMvc处理post请求乱码的filter
<filter> <filter-name>encodingFilter</filter-name> <filter-clas ...
- Calendar.getInstance()获取当天指定点上的时间[转载]
ctoday.add(Calendar.DAY_OF_MONTH, 1); 明天时间 //获得当天0点时间public static int getTimesmorning(){Calendar ca ...
- 二〇一五年五月二十二日--bug--启动页面出现模糊的问题
启动页面出现模糊的问题: 原因是 :android:theme="@style/TranslucentTheme" <application android:name=&qu ...
- HIT Winter Day ACM入门
A. Arpa’s hard exam and Mehrdad’s naive cheat 题意:统计1378^n的末尾数字 即统计8^n的末尾数字 n=0时为1 其他情况为{8,4,2,6}中的一个 ...
- linux设置时间服务器
对多个linux服务器,时间保持一致是很必要的.根据精确度要求,应该有相应的时间间隔进行时间同步.如果不进行时间同步,时间久了就会差别很大,遇到问题时定位就很困难.因为多台设备的配合,log之间可能有 ...
- LeetCode OJ 236. Lowest Common Ancestor of a Binary Tree
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...