看到一篇博文写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. BZOJ 3870: Our happy ending( 状压dp )

    dp(i, s)表示考虑了前i个数后, 能取到的数的集合为s时的方案数.对于1~min(L, K)枚举更新, 剩下的直接乘就好了. 复杂度O(T*K*2^N)...好像有点大, 但是可以AC.... ...

  2. 老旧Webkit浏览器行内元素0间距问题

    有时我们希望display:inline-block的元素之间的天衣无缝.紧密相依,比如说如下的情情形: 一般情况下我们使用如下代码可以实现: .pageNav { font-size:; text- ...

  3. PHP弱类型:WordPress Cookie伪造

    1 PHP弱类型 PHP是弱类型语言,所以变量会因为使用场景的不同自动进行类型转换.PHP中用 == 以及 != 进行相等判断时,会自动进行类型转换,用 === 以及 !== 进行判断时不会自动转换类 ...

  4. delphi R3下 跨进程获取DLL信息 NtQueryInformationProcess

    unit APIUnit; { GetProcessModuleHandle API Unit Ring3调用NtQueryInformationProcess实现跨进程获取DLL句柄 } inter ...

  5. WebWorker SharedWorker ServiceWorker

    WebWorker 是什么? 为 JavaScript 引入线程技术 不必再用 setTimeout().setInterval().XMLHttpRequest 来模拟并行 Worker 利用类似线 ...

  6. ETL工具之ODI

    ETL工具之ODI         到目前为止,Oracle的ETL工具包括两种,分别是Oracle Warehouse Builder(OWB)和Oracle Data Integrator(ODI ...

  7. java核心技术学习笔记之一程序设计概述

    Java 核心技术之一程序设计概述 一.   Java语言的特点 简单行 :取经于C++,排除了C++不常用的指针.结构等,增加垃圾回收. 面向对象:与C++不同是单继承,但是可以继承多接口.完全面向 ...

  8. android 中获取网络状态、判断3G、2G、wifi网络、判断wifi是否打开、获取本机地址、获取本机串号IMEI整理

    代码如下:package com.android.xym; import java.io.IOException; import java.net.HttpURLConnection; import ...

  9. Scale-up(纵向扩展) vs Scale-out(横向扩展)

    转载:http://wuaner.iteye.com/blog/1843799 http://www.javaworld.com/article/2077780/java-web-developmen ...

  10. android专栏

    Android之Activity(8) Android之Adapter(1) Android之ContentProvider(1) Android之Handler(4) Android之JSON(2) ...