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() ...
随机推荐
- 实现单例模式C++版本
还是先看最简单的C++单例模式 class CSingleton { private: CSingleton(){} static CSingleton *pInstance; public: sta ...
- Java中的流程控制(三)
关于Java中的流程控制 关于Java中的流程控制 4.do while语句 do while语句的功能和while语句差不多,只不过它是在执行完第一次循环后才检测条件表达式的值,这意味着包含在大括号 ...
- 原生js倒计时和显示当前时间
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- 使用css3和伪元素制作的一个立体导航条
使用css3和伪元素制作的一个立体导航条供大家参考,代码如下: <!doctype html> <html lang="en"> <head> ...
- bootstrap你让前端小狮子们又喜又恨
看了一个用bootstrap框架建的页面,简直方便至极!各种添加类就可以实现各种功能,各种添加data-toggle让你不需要懂javascript就可以做出很炫的效果! 最重要的它做出来的页面还是响 ...
- 《Pointers On C》读书笔记(第五章 操作符和表达式)
1.C语言操作符优先级表 2.算术操作符中%(取模操作符)只适用于整型类型,其余几个操作符(+.-.*./)既适用于整型类型也适用于浮点类型.当/操作符的两个操作数都是整型时,它执行整除运算,其它情况 ...
- JWPlayer 初探
http://www.360doc.com/content/13/0103/22/21412_258041878.shtml JWPlayer 是一款比较实用的web flash 播放器
- CentOS6.6普通用户使用sudo命令借用root用户权限
一.描写叙述 普通用户hadoop使用:tar -xzvf ns2.35.tar.gz命令解压文件,系统提示找不到该文件,无法打开该文件夹,于是想到使用sudo命令借用root用户的权限:sudo t ...
- 30天自制操作系统第八天学习笔记(u盘软盘双启动版本)
暑假学习小日本的那本书:30天自制操作系统 qq交流群:122358078 ,更多学习中的问题.资料,群里分享 environment:开发环境:ubuntu 第八天的学习思考: 关于鼠标是怎么 ...
- Objective-C中NSArray和NSMutableArray的基本用法
/*---------------------NSArray---------------------------*/ //创建数组 NSArray *array1 = [NSArray arrayW ...