看到一篇博文写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. 最近比较迷flash professional cc 做PPT,做一个flash做动态打字效果的教程

    想做一个flash打字效果.网上的方法要不是太繁琐,要不然就是各种遗漏.在这边做一个行之有效的flash做打字效果教程. 首先我用的是最新版本的flash professional cc .但是应该和 ...

  2. 10个必备的移动UI设计资源站

    http://www.uisdc.com/10-necessary-mobile-ui-design-resources# 交互设计中如何增加趣味性.提升愉悦http://www.uisdc.com/ ...

  3. sicily-1029 Rabbit

    一.      题意(0.04s) 每一对成熟的兔子可以生一对兔子,兔子在m个月之后成熟,假设兔子都不会死,计算d个月后一共有多少只兔子. 二.      要高精度加法(用string) 三.     ...

  4. How do I pull a native DOM element from a jQuery object? | jQuery Learning Center

    How do I pull a native DOM element from a jQuery object? | jQuery Learning Center How do I pull a na ...

  5. 张王李相亲应用if else

    package hello; public class to { public static void main(String[]args){ int a =1,b=0; int c =1,d=0; ...

  6. Github实例教程-创建库、创建主页

    以README文件为实例,具体介绍github的使用过程 请先下载git,然后配置下面内容: ( 我的系统是debian,其它版本号的UNIX/Linux有区别),windows的临时不清楚. (一) ...

  7. Shader程序中内置的状态变量

    经常在着色器程序需要访问一些全局状态,像当前的 model view projection 矩阵,当前环境的颜色诸如此类. 内置的矩阵 UNITY_MATRIX_MVP:当前模型 视窗 投影矩阵 UN ...

  8. [Jobdu] 题目1283:第一个只出现一次的字符

    题目描述: 在一个字符串(1<=字符串长度<=10000,全部由大写字母组成)中找到第一个只出现一次的字符. 输入: 输入有多组数据每一组输入一个字符串. 输出: 输出第一个只出现一次的字 ...

  9. BZOJ 1211: [HNOI2004]树的计数( 组合数学 )

    知道prufer序列就能写...就是求个可重集的排列...先判掉奇怪的情况, 然后答案是(N-2)!/π(d[i]-1)! -------------------------------------- ...

  10. 我用过的Linux命令--虚拟机和宿主机的网络连接方式

    VMWare提供了三种工作模式,它们是bridged(bridged模式:对应网卡vment0).NAT(网络地址转换模式:对应网卡vment8)和host-only(主机模式:对应网卡vment1) ...