看到一篇博文写lambda和reduce函数。笔者小痒了一下,用Python实现一下:

#! /usr/bin/env python

# -*-coding:utf-8-*-

import time

import math

def test_reduce():

start_time = time.clock()

print reduce[A1] (lambdax,y:x*y[A2] ,range(1,long(input('plz
input a num(>0):')+1))
[A3] )

print 'Time used:%s' %(time.clock()-start_time)

return;

def test_math():

start_time2 = time.clock()

print math.factorial[A4] (long(raw_input('plz
input a num(>0):')))

print 'Time used:%s' %(time.clock()-start_time2)

if __name__ == '__main__':

print '~'*34+'Use reduce'+'~'*34

test_reduce()

print '~'*34+'Use math'+'~'*34

test_math()

Python is Python!


[A1]关于reduce函数的參数及解释:

reduce(function, iterable[, initializer])

Apply function of two argumentscumulatively to the items of iterable, from left to right, so as to reduce theiterable to a single value. For example, reduce(lambda x, y: x+y, [1, 2, 3, 4,5]) calculates ((((1+2)+3)+4)+5). The left argument, x, is the accumulatedvalue
and the right argument, y, is the update value from the iterable. If theoptional initializer is present, it is placed before the items of the iterablein the calculation, and serves as a default when the iterable is empty. Ifinitializer is not given and iterable
contains only one item, the first item isreturned.

[A2]关于lambda函数的參数及解释:

An anonymous inline function consisting ofa single expressionwhich is evaluated when the function is called. The syntax to create a lambdafunction is lambda [arguments]: expression

Lambda expressions (sometimes called lambdaforms) have the same syntactic position as expressions. They are a shorthand tocreate anonymous functions; the expression lambda arguments: expression yieldsa function object. The unnamed object behaves like a function
object definedwith

def name(arguments):

return expression

[A3]关于range函数的參数及解释:

the built-in function range() returns a sequence of integers suitable to emulate theeffect of Pascal’s for i := a to b do; e.g., range(3) returns the list [0, 1,2].

[A4]关于factorial函数的參数及解释:

math.factorial(x)

Return x factorial. Raises ValueError if x is not integral or is negative.

Python lambda和reduce函数的更多相关文章

  1. Python之利用reduce函数求序列的最值及排序

    在一般将Python的reduce函数的例子中,通常都是拿列表求和来作为例子.那么,是否还有其他例子呢?   本次分享将讲述如何利用Python中的reduce函数对序列求最值以及排序.   我们用r ...

  2. Python 3里,reduce()函数已经被从全局名字空间里移除了,它现在被放置在fucntools模块里

    reduce函数:在Python 3里,reduce()函数已经被从全局名字空间里移除了,它现在被放置在fucntools模块里 用的话要 先引入:>>> from functool ...

  3. Python map,filter,reduce函数

    # -*- coding:utf-8 -*- #定义一个自己的map函数list_list = [1,2,4,8,16] def my_map(func,iterable): my_list = [] ...

  4. Python lambda(匿名函数)介绍【转】

    引用: http://www.cnblogs.com/evening/archive/2012/03/29/2423554.html 在学习python的过程中,lambda的语法时常会使人感到困惑, ...

  5. Python: lambda, map, reduce, filter

    在学习python的过程中,lambda的语法时常会使人感到困惑,lambda是什么,为什么要使用lambda,是不是必须使用lambda? 下面就上面的问题进行一下解答. 1.lambda是什么? ...

  6. python里使用reduce()函数

    reduce()函数在库functools里,如果要使用它,要从这个库里导入.reduce函数与map函数有不一样地方,map操作是并行操作,reduce函数是把多个参数合并的操作,也就是从多个条件简 ...

  7. python Map()和reduce()函数

    Map()和reduce()函数 map() 会根据提供的函数对指定序列做映射. 第一个参数 function 以参数序列中的每一个元素调用 function 函数,返回包含每次 function 函 ...

  8. python中的reduce函数

    python中的reduce   python中的reduce内建函数是一个二元操作函数,他用来将一个数据集合(链表,元组等)中的所有数据进行下列操作:用传给reduce中的函数 func()(必须是 ...

  9. python中filter(),reduce()函数

    filter()函数 是 Python 内置的另一个有用的高阶函数,filter()函数接收一个函数 和一个list,这个函数的作用是对每个元素进行判断,返回 True或 False,filter() ...

随机推荐

  1. SGU 134.Centroid( 树形dp )

    一道入门树dp, 求一棵树的重心...我是有多无聊去写这种题...傻X题写了也没啥卵用以后还是少写好.. ----------------------------------------------- ...

  2. JavaScript中的闭包理解

    原创文章,转载请注明:JavaScript中的闭包理解  By Lucio.Yang 1.JavaScript闭包 在小学期开发项目的时候,用node.js开发了服务器,过程中遇到了node.js的第 ...

  3. 常用Java片段

    1. 字符串与整型的相互转换 String a = String.valueOf(2);   //integer to numeric string int i = Integer.parseInt( ...

  4. android-服务Service

    服务是在后台运行,负责更新内容提供器.发出意图.触发通知,它们是执行持续或定时处理的方式. 多线程一般捆绑服务执行任务,因为在activity中开辟多线程执行任务的话,子线程的生命周期得不到保障,可能 ...

  5. nyoj 21三个水杯(BFS + 栈)

    题目链接: http://acm.nyist.net/JudgeOnline/problem.php?pid=21 思想: 看了一下搜索就来写了这题(BFS 找出最短路径 所以用此来进行搜索) 这题在 ...

  6. JS 节流阀

    JS 节流阀 参考 https://github.com/hahnzhu/read-code-per-day/issues/5 节流阀 节流阀的基本原理 事件函数的执行都记下当前时间, 只有当前时间与 ...

  7. 简单makefile的写法

    一个项目下的文件比较多,如果单个的输入,比较费劲,所以就需要把编译过程写进一个MakeFile文件中. 下面建立5个文件,3个cxx文件,2个hxx头文件 //filename main.cxx #i ...

  8. PHP cURL 应用

    对于做过数据采集的人来说,cURL一定不会陌生.虽然在PHP中有 file_get_contents函数可以获取远程链接的数据,但是它的可控制性太差了,对于各种复杂情况的采集情 景,file_get_ ...

  9. 浅析JQuery中的html(),text(),val()区别

    1.HTML html():取得第一个匹配元素的html内容.这个函数不能用于XML文档.但可以用于XHTML文档 html(val):设置每一个匹配元素的html内容.这个函数不能用于XML文档.但 ...

  10. Lua环境配置 windows + VS

    环境搭建: 首先从 http://www.lua.org/ftp/下载lua 源码, 我选择的是lua-5.1.5.tar.gz 我的开发环境是Win7+ VS2010 打开VS2010新建一个工程L ...