1. 作用

快速创建匿名单行最小函数,对数据进行简单处理, 常常与 map(), reduce(), filter() 联合使用。

2. 与一般函数的不同

>>> def f (x): return x**2
...
>>> print f(8)
64
>>>
>>> g = lambda x: x**2 # 匿名函数默认冒号前面为参数(多个参数用逗号分开), return 冒号后面的表达式
>>>
>>> print g(8)
64

3. 与一般函数嵌套使用

与一般函数嵌套,可以产生多个功能类似的的函数。

>>> def make_incrementor (n): return lambda x: x + n
>>>
>>> f = make_incrementor(2)
>>> g = make_incrementor(6)
>>>
>>> print f(42), g(42)
44 48
>>>
>>> print make_incrementor(22)(33)
55

4. 在 sequence 中的使用

filter(function or None, sequence) -> list, tuple, or string

map(function, sequence[, sequence, ...]) -> list

reduce(function, sequence[, initial]) -> value

>>> foo = [2, 18, 9, 22, 17, 24, 8, 12, 27]
>>>
>>> print filter(lambda x: x % 3 == 0, foo)
[18, 9, 24, 12, 27]
>>>
>>> print map(lambda x: x * 2 + 10, foo)
[14, 46, 28, 54, 44, 58, 26, 34, 64]
>>>
>>> print reduce(lambda x, y: x + y, foo) # return (((((a+b)+c)+d)+e)+f) equal to sum()
139

5. 两个例子

a) 求素数

>>> nums = range(2, 50)
>>> for i in range(2, 8):
... nums = filter(lambda x: x == i or x % i, nums)
...
>>> print nums
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]

注释: "Leave the element in the list if it is equal to i, or if it leaves a non-zero remainder when divided by i. Otherwise remove it from the list." 从 2 到 7 循环,每次把倍数移除。

b) 求单词长度

>>> sentence = 'It is raining cats and dogs'
>>> words = sentence.split()
>>> print words
['It', 'is', 'raining', 'cats', 'and', 'dogs']
>>>
>>> lengths = map(lambda word: len(word), words)
>>> print lengths
[2, 2, 7, 4, 3, 4]

参考资料:

http://www.secnetix.de/olli/Python/lambda_functions.hawk

Python: Lambda Functions的更多相关文章

  1. Python Lambda & Functional Programming

    Python Lambda & Functional Programming 函数式编程 匿名函数 纯函数 高阶函数 # higher-order functions def apply_tw ...

  2. python lambda函数详细解析(面试经常遇到)

    1 作用:通常是用来在python中创建匿名函数的 2 格式: lambda 参数[,参数] : 表达式 3 注意: (1)lambda定义的是单行函数, 如果需要复杂的函数,应该定义普通函数 (2) ...

  3. Python lambda 表达式有何用处,如何使用?

    在看Python教程的时候,被 lambda 的解释给难住了,之前没有这么用过. 在<简明Python教程>上的解释: [摘录如下:] lambda语句被用来创建新的函数对象,并且在运行时 ...

  4. Python lambda介绍

    转自:http://www.cnblogs.com/evening/archive/2010/03/29/2423554.html Python lambda 介绍   在学习python的过程中,l ...

  5. python lambda简单介绍

    python lambda 在python中,如果想要创建函数需要使用关键字def,而如果想要创建匿名函数,就需要使用lambda. lambda创建的函数和def创建的函数有什么区别? def创建的 ...

  6. Python Lambda 的简单用法

    下面代码简单举例介绍以下 lambda的用法. from functools import reduce #1 python lambda会创建一个函数对象,但不会把这个函数对象赋给一个标识符,而de ...

  7. python lambda表达式简单用法【转】

    python lambda表达式简单用法 1.lambda是什么? 看个例子: g = lambda x:x+1 看一下执行的结果: g(1) >>>2 g(2) >>& ...

  8. Python lambda和reduce函数

    看到一篇博文写lambda和reduce函数.笔者小痒了一下,用Python实现一下: #! /usr/bin/env python # -*-coding:utf-8-*- import time ...

  9. C++闭包: Lambda Functions in C++11

    表达式无疑是C++11最激动人心的特性之一!它会使你编写的代码变得更优雅.更快速! 它实现了C++11对于支持闭包的支持.首先我们先看一下什么叫做闭包 维基百科上,对于闭包的解释是: In progr ...

随机推荐

  1. [转]Flash ActionScript2.0面向对象游戏开发-推箱子

    本文转自:http://www.alixixi.com/Dev/W3C/Flash/2007/2007070868666.html 概述: Flash ActionScript2.0是一种面向对向的编 ...

  2. CentOS 7 安装Redis 2.8.7

    1.下载软件: wget wget http://download.redis.io/releases/redis-2.8.7.tar.gz 2.解压软件并编译安装: tar -zxvf redis- ...

  3. zookeeper中Watcher和Notifications

    问题导读:1.zookeeper观察者什么时候调用?2.传统远程轮询服务存在什么问题?3.zk中回调服务的机制是什么?4.zk中watcher为什么不永久注册?5.什么是znode? 在阅读之前首先明 ...

  4. Visualize real-time data streams with Gnuplot

    源文地址 (September 2008) For the last couple of years, I've been working on European Space Agency (ESA) ...

  5. 【iOS 初见】第一个简单的 iOS 应用

    本实例来自 <iOS编程(第4版)>,介绍如何编写一个简单的 iOS 应用. 功能为:在视图中显示一个问题,用户点击视图下方的按钮,可以显示相应的答案,用户点击上方的按钮,则会显示一个新的 ...

  6. [cocos2dx]让CCScrollView支持分页

    [cocos2dx]让CCScrollView支持分页 做过IOS开发的朋友, 肯定知道UIScrollView有一个isPaged属性. 当设置其为true的时候, 滑动会自动分页. 即, 每次滑动 ...

  7. codeforces 616E Sum of Remainders (数论,找规律)

    E. Sum of Remainders time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  8. python中的getattr函数

    getattr(object, name[, default]) -> value Get a named attribute from an object; getattr(x, 'y') i ...

  9. HDU 4819 Mosaic --二维线段树(树套树)

    题意: 给一个矩阵,每次查询一个子矩阵内的最大最小值,然后更新子矩阵中心点为(Max+Min)/2. 解法: 由于是矩阵,且要求区间最大最小和更新单点,很容易想到二维的线段树,可是因为之前没写过二维的 ...

  10. Mango Weekly Training Round #3 解题报告

    A. Codeforces 92A Chips 签到题.. #include <iostream> #include <cstdio> #include <cstring ...