Python lambda和reduce函数
2016-02-05 20:04
429 查看
看到一篇博文写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.
#! /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教程学习简记4--Python 迭代(iteration)
- 《Python核心编程》第四章:Python 对象
- 《Python核心编程》第三章:Python基础
- 《Python核心编程》第二章:Python起步
- Python教程学习简记3--Python 切片
- 如何让python程序运行得更快
- python文件读写的小笔记
- python小爬虫
- 《Python核心编程》第一章:欢迎来到Python世界!
- Python笔记:使用pywin32处理excel文件
- python35
- 迭代器
- [Leetcode] 179. Largest Number @python
- python 11期 第八天
- [Leetcode]174. Dungeon Game @python
- 2.4鼠标事件
- python编码问题
- Beginning Python Chapter 2 Notes
- python argparse模块解析命令行选项简单使用
- [Leetcode]166. Fraction to Recurring Decimal @python