Python:通过计算阶乘来学习lambda和reduce这两个函数的使用
从上学时开始,通常是用C来求阶乘,今天无事,用python写了一下,主要在于学习lambda和reduce这两个函数的使用。
实现:
[python] #!/usr/bin/env python
#-*- coding: utf-8 -*-
import time
def test_factorial_reduce():
'''''
Function:使用reduce函数
Input:NONE
Output: NONE
author: socrates
blog:http://blog.csdn.net/dyx1024
date:2012-02-19
'''
time_begin = time.clock()
print reduce(lambda x,y:x*y, range(1, long(raw_input("please input a number ( > 0):"))))
print "Use time: %s" % (time.clock() - time_begin)
return;
def test_factorial_math():
'''''
Function:使用math库函数
Input:NONE
Output: NONE
author: socrates
blog:http://blog.csdn.net/dyx1024
date:2012-02-19
'''
import math
time_begin = time.clock()
print math.factorial(long(raw_input("please input a number ( > 0):")))
print "Use time: %s" % (time.clock() - time_begin)
if __name__ == '__main__':
print '*' * 50 + "Use reduce" + '*' * 50
test_factorial_reduce()
print '*' * 50 + "Use math api" + '*' * 50
test_factorial_math()
#!/usr/bin/env python
#-*- coding: utf-8 -*-
import time
def test_factorial_reduce():
'''
Function:使用reduce函数
Input:NONE
Output: NONE
author: socrates
blog:http://blog.csdn.net/dyx1024
date:2012-02-19
'''
time_begin = time.clock()
print reduce(lambda x,y:x*y, range(1, long(raw_input("please input a number ( > 0):"))))
print "Use time: %s" % (time.clock() - time_begin)
return;
def test_factorial_math():
'''
Function:使用math库函数
Input:NONE
Output: NONE
author: socrates
blog:http://blog.csdn.net/dyx1024
date:2012-02-19
'''
import math
time_begin = time.clock()
print math.factorial(long(raw_input("please input a number ( > 0):")))
print "Use time: %s" % (time.clock() - time_begin)
if __name__ == '__main__':
print '*' * 50 + "Use reduce" + '*' * 50
test_factorial_reduce()
print '*' * 50 + "Use math api" + '*' * 50
test_factorial_math()
测试:
[plain] **************************************************Use reduce**************************************************
please input a number ( > 0):50
608281864034267560872252163321295376887552831379210240000000000
Use time: 3.3163365735
**************************************************Use math api**************************************************
please input a number ( > 0):50
30414093201713378043612608166064768844377641568960512000000000000
Use time: 3.70409004496
**************************************************Use reduce**************************************************
please input a number ( > 0):50
608281864034267560872252163321295376887552831379210240000000000
Use time: 3.3163365735
**************************************************Use math api**************************************************
please input a number ( > 0):50
30414093201713378043612608166064768844377641568960512000000000000
Use time: 3.70409004496
我们感兴趣的不在程序本身,我想更多地会关注lambda和reduce这两个函数,我们来看一下:
lambda
手册中这样描述:
[plain] lambda
An anonymous inline function consisting of a single expression which is evaluated when the function is called. The syntax to create a lambda function is lambda [arguments]: expression
lambda
An anonymous inline function consisting of a single expression which is evaluated when the function is called. The syntax to create a lambda function is lambda [arguments]: expression 其实lambda就是个匿名函数,它本身是一个表达式,而def为一个语句,这就是说lambda可以用于函数中做为参数等,但def这个语句不能。
我们的 lambda x,y:x*y 语句等价于下面这个函数:
[python] def my_func(x, y):
return x * y
def my_func(x, y):
return x * y
reduce
手册中这样描述:
[plain] reduce(function, iterable[, initializer])
Apply function of two arguments cumulatively to the items of iterable, from left to right, so as to reduce the iterable 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 accumulated value and the right argument, y, is the update value from the iterable. If the optional initializer is present, it is placed before the items of the iterable in the calculation, and serves as a default when the iterable is empty. If initializer is not given and iterable contains only one item, the first item is returned.
reduce(function, iterable[, initializer])
Apply function of two arguments cumulatively to the items of iterable, from left to right, so as to reduce the iterable 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 accumulated value and the right argument, y, is the update value from the iterable. If the optional initializer is present, it is placed before the items of the iterable in the calculation, and serves as a default when the iterable is empty. If initializer is not given and iterable contains only one item, the first item is returned.
就是用函数function对序列,如list中的元素进行处理,每次处理两个数据项(一个是前次处理的结果,一个是序列中的下一个元素),如此反复的递归处理,最后对整个序列求出一个单一的返回值。
改写一下上面的程序,很快可以理解这句话的意思:
[python] def my_func(x, y):
补充:Web开发 , Python ,