Python lambda和reduce函数
看到一篇博文写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
[A4]关于factorial函数的參数及解释:
math.factorial(x)
Return x factorial. Raises ValueError if x is not integral or is negative.
Python lambda和reduce函数的更多相关文章
- Python之利用reduce函数求序列的最值及排序
在一般将Python的reduce函数的例子中,通常都是拿列表求和来作为例子.那么,是否还有其他例子呢? 本次分享将讲述如何利用Python中的reduce函数对序列求最值以及排序. 我们用r ...
- Python 3里,reduce()函数已经被从全局名字空间里移除了,它现在被放置在fucntools模块里
reduce函数:在Python 3里,reduce()函数已经被从全局名字空间里移除了,它现在被放置在fucntools模块里 用的话要 先引入:>>> from functool ...
- Python map,filter,reduce函数
# -*- coding:utf-8 -*- #定义一个自己的map函数list_list = [1,2,4,8,16] def my_map(func,iterable): my_list = [] ...
- Python lambda(匿名函数)介绍【转】
引用: http://www.cnblogs.com/evening/archive/2012/03/29/2423554.html 在学习python的过程中,lambda的语法时常会使人感到困惑, ...
- Python: lambda, map, reduce, filter
在学习python的过程中,lambda的语法时常会使人感到困惑,lambda是什么,为什么要使用lambda,是不是必须使用lambda? 下面就上面的问题进行一下解答. 1.lambda是什么? ...
- python里使用reduce()函数
reduce()函数在库functools里,如果要使用它,要从这个库里导入.reduce函数与map函数有不一样地方,map操作是并行操作,reduce函数是把多个参数合并的操作,也就是从多个条件简 ...
- python Map()和reduce()函数
Map()和reduce()函数 map() 会根据提供的函数对指定序列做映射. 第一个参数 function 以参数序列中的每一个元素调用 function 函数,返回包含每次 function 函 ...
- python中的reduce函数
python中的reduce python中的reduce内建函数是一个二元操作函数,他用来将一个数据集合(链表,元组等)中的所有数据进行下列操作:用传给reduce中的函数 func()(必须是 ...
- python中filter(),reduce()函数
filter()函数 是 Python 内置的另一个有用的高阶函数,filter()函数接收一个函数 和一个list,这个函数的作用是对每个元素进行判断,返回 True或 False,filter() ...
随机推荐
- hadoop搭建杂记:Linux下不同linux主机之间文件copy的scp命令
不同的Linux之间copy文件常用有3种方法: 不同的Linux之间copy文件常用有3种方法: ①ftp 就是其中一台Linux安装ftp Server,这样可以另外一台使用ftp的程序来进行文件 ...
- Android 内部存储安装apk文件实现
目前国内市场的山寨机横行,安卓手机升级也是一天一个样,对于原来老手机可能没有SDCARD,导致我们的APP不能下载资源,无法更新APP,针对这种情况有以下解决方案.通过以下函数判断是否有SD卡再判断下 ...
- ThinPHP第二十七天(kindEditor使用,$.each)
1.KindEditor简单使用实例 <js file="__PUBLIC__/kindeditor/kindeditor.js" /> <js file=&qu ...
- call和apply方法的理解
第一种:直接先来个粟子吧... ; } var child = {}; console.log(child); //Object() 对象 Parent.call(child); console. ...
- java多线程同步
一篇好文:java多线程机制同步原则 概括起来说,Java 多线程同步机制主要包含如下几点:1:如果一个类包含一个或几个同步方法,那么由此类生成的每一个对象都配备一个队列用来容纳那些等待执行同步的线程 ...
- php执行shell更新svn文件的方法
vim /etc/sudoers 修改内容如下: #Defaults !visiblepw Defaults visiblepw #Defaults requiretty <?php set_t ...
- mysql外键使用和级联
如下面的: create table applicant (id int not null auto_increment primary key, jobId int not null, studen ...
- POJ 1743 Musical Theme(后缀数组+二分答案)
[题目链接] http://poj.org/problem?id=1743 [题目大意] 给出一首曲子的曲谱,上面的音符用不大于88的数字表示, 现在请你确定它主旋律的长度,主旋律指的是出现超过一次, ...
- 如何打开Nib文件
作者:ani_di 版权所有,转载务必保留此链接 http://blog.csdn.net/ani_di # 如何打开Nib文件 有段时间没有灌水,今天分享一个小技巧. nib文件是Cocoa Ap ...
- 图画hadoop -- 生态圈