Python:通过计算阶乘来学习lambda和reduce这两个函数(2)

[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  


我们感兴趣的不在程序本身,我想更多地会关注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就是个匿名函数,它本身是一个表达式,而def为一个语句,这就是说lambda可以用于函数中做为参数等,但def这个语句不能。

我们的 lambda x,y:x*y 语句等价于下面这个函数:

[python]

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.  

就是用函数function对序列,如list中的元素进行处理,每次处理两个数据项(一个是前次处理的结果,一个是序列中的下一个元素),如此反复的递归处理,最后对整个序列求出一个单一的返回值。

改写一下上面的程序,很快可以理解这句话的意思:

[python]

def my_func(x, y):       print 'x = %d, y = %d' %(x, y)       return x * y      print reduce(my_func, range(1, long(raw_input("please input a number ( > 0):"))))  

运行结果:

[plain]

please input a number ( > 0):50   x = 1, y = 2   x = 2, y = 3   x = 6, y = 4   x = 24, y = 5   x = 120, y = 6   x = 720, y = 7   x = 5040, y = 8   x = 40320, y = 9   x = 362880, y = 10   x = 3628800, y = 11   x = 39916800, y = 12   x = 479001600, y = 13   x = 6227020800, y = 14   x = 87178291200, y = 15   x = 1307674368000, y = 16   x = 20922789888000, y = 17   x = 355687428096000, y = 18   x = 6402373705728000, y = 19   x = 121645100408832000, y = 20   x = 2432902008176640000, y = 21   x = 51090942171709440000, y = 22   x = 1124000727777607680000, y = 23   x = 25852016738884976640000, y = 24   x = 620448401733239439360000, y = 25   x = 15511210043330985984000000, y = 26   x = 403291461126605635584000000, y = 27   x = 10888869450418352160768000000, y = 28   x = 304888344611713860501504000000, y = 29   x = 8841761993739701954543616000000, y = 30   x = 265252859812191058636308480000000, y = 31   x = 8222838654177922817725562880000000, y = 32   x = 263130836933693530167218012160000000, y = 33   x = 8683317618811886495518194401280000000, y = 34   x = 295232799039604140847618609643520000000, y = 35   x = 10333147966386144929666651337523200000000, y = 36   x = 371993326789901217467999448150835200000000, y = 37   x = 13763753091226345046315979581580902400000000, y = 38   x = 523022617466601111760007224100074291200000000, y = 39   x = 20397882081197443358640281739902897356800000000, y = 40   x = 815915283247897734345611269596115894272000000000, y = 41   x = 33452526613163807108170062053440751665152000000000, y = 42   x = 1405006117752879898543142606244511569936384000000000, y = 43   x = 60415263063373835637355132068513997507264512000000000, y = 44   x = 2658271574788448768043625811014615890319638528000000000, y = 45   x = 119622220865480194561963161495657715064383733760000000000, y = 46   x = 5502622159812088949850305428800254892961651752960000000000, y = 47   x = 258623241511168180642964355153611979969197632389120000000000, y = 48   x = 12413915592536072670862289047373375038521486354677760000000000, y = 49   608281864034267560872252163321295376887552831379210240000000000  

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wyzydy.html