lambda函数是一种快速定义单行最小函数的方法,是从Lisp借鉴而来的,可以用在任何需要函数的地方。

基础

lambda语句中,冒号前是参数,可以有多个,用逗号分割;冒号右边是返回值。 
lambda语句构建的是一个函数对象。

# 两个参数,x和y,返回两个参数的和
>>> f = lambda x, y: x+y
>>> type(f)
<type 'function'>
>>> f
<function <lambda> at 0x7f6d023000c8>
>>> f(10, 12)
22
>>> f("hello ", "world")
'hello world'

map

map(…) 函数官方文档 
map函数结果生成一个list,参数为函数、一个或多个序列;如果函数的参数只有一个,那么应该有一个序列,有多个参数,那么应该有相应数量的序列。 
简单的说:map(function,sequence) :对sequence中的item依次执行function(item),见执行结果组成一个List返回 
map(function, sequence[, sequence, …]) -> list

Return a list of the results of applying the function to the items of the argument sequence(s).  If more than one sequence is given, the function is called with an argument list consisting of the corresponding item of each sequence, substituting None for missing values when not all sequences have the same length.  If the function is None, return a list of the items of the sequence (or a list of tuples if more than one sequence).
  • 1
  • 2

>>> map(lambda x: x*2, range(1,10))
[2, 4, 6, 8, 10, 12, 14, 16, 18]
>>> map(lambda x, y: x*y, range(1,10), range(2, 11))
[2, 6, 12, 20, 30, 42, 56, 72, 90]
>>> map(lambda x: 1, range(1,10))
[1, 1, 1, 1, 1, 1, 1, 1, 1]

官方文档中最后一句解释,特别重要: map函数的参数中有一个是function(函数),这个函数也可以是None(那句话的意思不是返回值是None)。如果是None的话,map就与zip函数类似了,看下面的例子, 区别已经在zip函数介绍过了。

>>> map(None, range(10), range(1, 11))
[(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 7), (7, 8), (8, 9), (9, 10)]
>>> zip(range(10), range(1, 11))
[(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 7), (7, 8), (8, 9), (9, 10)]
>>> zip(range(10))
[(0,), (1,), (2,), (3,), (4,), (5,), (6,), (7,), (8,), (9,)]
>>> map(None, range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

因此,map函数的执行过程,……不好描述啊……

reduce

reduce(function,sequence):对sequence中的item顺序迭代调用function。 
reduce(…) 官方文档 
reduce(function, sequence[, initial]) -> value 
Apply a function of two arguments cumulatively to the items of a sequence, from left to right, so as to reduce the sequence to a single value. For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates ((((1+2)+3)+4)+5). If initial is present, it is placed before the items of the sequence in the calculation, and serves as a default when the sequence is empty. 
根据文档里面显示的代码:

>>> reduce(lambda x, y: x+y, [1, 2, 3, 4, 5])
15
# 内部执行过程,((((1+2)+3)+4)+5) # list只有一个元素
>>> reduce(lambda x: x+2, [1])
1 #reduce 如果list的长度大于1,lambda表达式必须有两个参数(大于2个参数是不对的)
>>> reduce(lambda x: x+2, [1, 2, 3, 4, 5])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: <lambda>() takes exactly 1 argument (2 given)

filter

filter(function,sequence):对sequence中的item依次执行function(item),将执行结果为True的item组成一个List/String/Tuple(取决于sequence的类型)返回。

>>> filter(lambda x:x>10, range(1,20))
[11, 12, 13, 14, 15, 16, 17, 18, 19] >>> filter(lambda s: s != 'a' , 'abcdefg')
'bcdefg'

lambda表达式 匿名函数的更多相关文章

  1. [二] java8 函数式接口详解 函数接口详解 lambda表达式 匿名函数 方法引用使用含义 函数式接口实例 如何定义函数式接口

    函数式接口详细定义 package java.lang; import java.lang.annotation.*; /** * An informative annotation type use ...

  2. java8函数式接口详解、函数接口详解、lambda表达式匿名函数、方法引用使用含义、函数式接口实例、如何定义函数式接口

    函数式接口详细定义 函数式接口只有一个抽象方法 由于default方法有一个实现,所以他们不是抽象的. 如果一个接口定义了一个抽象方法,而他恰好覆盖了Object的public方法,仍旧不算做接口的抽 ...

  3. Python:lambda表达式(匿名函数)

    lambda表达式: 通常是在需要一个函数,但是又不想费神去命名一个函数的场合下使用,也就是指匿名函数. 当我们在传入函数时,有些时候,不需要显式地定义函数,直接传入匿名函数更方便. 在Python中 ...

  4. lambda表达式匿名函数

    匿名函数是一个“内联”语句或表达式,可在需要委托类型的任何地方使用.可以使用匿名函数来初始化命名委托,或传递命名委托(而不是命名委托类型)作为方法参数. C# 中委托的发展 在 C# 1.0 中,您通 ...

  5. C++中对C的扩展学习新增语法——lambda 表达式(匿名函数)

    1.匿名函数基础语法.调用.保存 1.auto lambda类型 2.函数指针来保存注意点:[]只能为空,不能写东西 3.std::function来保存 2.匿名函数捕捉外部变量(值方式.引用方式) ...

  6. 【C++】C++中的lambda表达式和函数对象

    目录结构: contents structure [-] lambda表达式 lambda c++14新特性 lambda捕捉表达式 泛型lambda表达式 函数对象 函数适配器 绑定器(binder ...

  7. Lambda表达式匿名类实现接口方法

    Lamb表达式匿名类实现接口方法 import java.util.ArrayList; public class HandlerDemo{ public static void main(Strin ...

  8. Lambda表达式和函数试接口的最佳实践 · LiangYongrui's Studio

    1.概述 本文主要深入研究java 8中的函数式接口和Lambda表达式,并介绍最佳实践. 2.使用标准的函数式接口 包java.util.function中的函数是接口已经可以满足大部分的java开 ...

  9. 十二、C# 委托与Lambda表达式(匿名方法的另一种写法)

    委托与Lambda表达式   1.委托概述 2.匿名方法 3.语句Lambda 4.表达式Lambda 5.表达式树   一.委托概述 相当于C++当中的方法指针,在C#中使用delegate 委托来 ...

随机推荐

  1. python 利用split读取文本文件中每一行的数字并保存至相应文件夹

    import re from numpy import * def getStr(file_path,file_path1): fp = open(file_path, 'r') op = open( ...

  2. [转] 最详尽的 JS 原型与原型链终极详解

    四. __proto__ JS 在创建对象(不论是普通对象还是函数对象)的时候,都有一个叫做__proto__ 的内置属性,用于指向创建它的构造函数的原型对象. 对象 person1 有一个 __pr ...

  3. js数字货币格式互转

    //将1,234,567.00转换为1234567.00 function moneyToNumValue(val) { var num = val.trim(); var ss = num.toSt ...

  4. [转]CR, LF, CR/LF区别与关系

    http://weizhifeng.net/talking-about-cr-lf.html 前言 在文本处理中,CR(Carriage Return),LF(Line Feed),CR/LF是不同操 ...

  5. Python_冒泡排序

    从小到大的排序:(最前面的数和一步步和后面的数比较,如果大于则交换,如果不大于则继续循环) 方法1: data = [65, 1, 45, 77, 3, 9, 43, 23, 7, 53, 213, ...

  6. LOJ#6433. 「PKUSC2018」最大前缀和 状压dp

    原文链接https://www.cnblogs.com/zhouzhendong/p/LOJ6433.html 题解 枚举一个集合 S ,表示最大前缀和中包含的元素集为 S ,然后求出有多少个排列是这 ...

  7. 阿里巴巴Java开发规范手册

      Java开发手册 版本号 制定团队 更新日期 备  注 1.0.0 阿里巴巴集团技术部 2016.12.7 首次向Java业界公开 一.编程规约 (一) 命名规约 1.   [强制]所有编程相关命 ...

  8. spring mvc中的service和controller中读取不到properties值

    根据web.xml读取配置文件中的顺序来看 controller层和service层来自于spring mvc.xml中读取,所以必须要在spring mvc.xml中配置读取资源文件夹方式

  9. B - Glider Gym - 101911B(二分)

    output standard output A plane is flying at a constant height of hh meters above the ground surface. ...

  10. Linux学习之日志管理(二十一)

    Linux学习之日志管理 目录 日志管理 日志服务 rsyslogd的新特点 启动日志服务 常见日志的作用 日志文件的一般格式 rsyslogd日志服务 /etc/rsyslog.conf配置文件 服 ...