设为首页 收藏本站
查看: 827|回复: 0

[经验分享] Python:通过计算阶乘来学习lambda和reduce这两个函数的使用

[复制链接]

尚未签到

发表于 2017-5-8 10:27:55 | 显示全部楼层 |阅读模式
从上学时开始,通常是用C来求阶乘,今天无事,用python写了一下,主要在于学习lambda和reduce这两个函数的使用。  实现:
#!/usr/bin/env python#-*- coding: utf-8 -*-import timedef test_factorial_reduce():'''Function:使用reduce函数Input:NONEOutput: NONEauthor: socratesblog:http://blog.csdn.net/dyx1024date: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:NONEOutput: NONEauthor: socratesblog:http://blog.csdn.net/dyx1024date:2012-02-19'''     import mathtime_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" + '*' * 50test_factorial_reduce()print '*' * 50 + "Use math api" + '*' * 50test_factorial_math()  测试:
**************************************************Use reduce**************************************************please input a number ( > 0):50608281864034267560872252163321295376887552831379210240000000000Use time: 3.3163365735**************************************************Use math api**************************************************please input a number ( > 0):5030414093201713378043612608166064768844377641568960512000000000000Use time: 3.70409004496

  我们感兴趣的不在程序本身,我想更多地会关注lambda和reduce这两个函数,我们来看一下:
  lambda

  手册中这样描述:
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 语句等价于下面这个函数:
def my_func(x, y):return x * y
  reduce
  手册中这样描述:
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中的元素进行处理,每次处理两个数据项(一个是前次处理的结果,一个是序列中的下一个元素),如此反复的递归处理,最后对整个序列求出一个单一的返回值。  改写一下上面的程序,很快可以理解这句话的意思:
def my_func(x, y):print 'x = %d, y = %d' %(x, y)return x * yprint reduce(my_func, range(1, long(raw_input("please input a number ( > 0):"))))
运行结果:please input a number ( > 0):50x = 1, y = 2x = 2, y = 3x = 6, y = 4x = 24, y = 5x = 120, y = 6x = 720, y = 7x = 5040, y = 8x = 40320, y = 9x = 362880, y = 10x = 3628800, y = 11x = 39916800, y = 12x = 479001600, y = 13x = 6227020800, y = 14x = 87178291200, y = 15x = 1307674368000, y = 16x = 20922789888000, y = 17x = 355687428096000, y = 18x = 6402373705728000, y = 19x = 121645100408832000, y = 20x = 2432902008176640000, y = 21x = 51090942171709440000, y = 22x = 1124000727777607680000, y = 23x = 25852016738884976640000, y = 24x = 620448401733239439360000, y = 25x = 15511210043330985984000000, y = 26x = 403291461126605635584000000, y = 27x = 10888869450418352160768000000, y = 28x = 304888344611713860501504000000, y = 29x = 8841761993739701954543616000000, y = 30x = 265252859812191058636308480000000, y = 31x = 8222838654177922817725562880000000, y = 32x = 263130836933693530167218012160000000, y = 33x = 8683317618811886495518194401280000000, y = 34x = 295232799039604140847618609643520000000, y = 35x = 10333147966386144929666651337523200000000, y = 36x = 371993326789901217467999448150835200000000, y = 37x = 13763753091226345046315979581580902400000000, y = 38x = 523022617466601111760007224100074291200000000, y = 39x = 20397882081197443358640281739902897356800000000, y = 40x = 815915283247897734345611269596115894272000000000, y = 41x = 33452526613163807108170062053440751665152000000000, y = 42x = 1405006117752879898543142606244511569936384000000000, y = 43x = 60415263063373835637355132068513997507264512000000000, y = 44x = 2658271574788448768043625811014615890319638528000000000, y = 45x = 119622220865480194561963161495657715064383733760000000000, y = 46x = 5502622159812088949850305428800254892961651752960000000000, y = 47x = 258623241511168180642964355153611979969197632389120000000000, y = 48x = 12413915592536072670862289047373375038521486354677760000000000, y = 49608281864034267560872252163321295376887552831379210240000000000

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-374555-1-1.html 上篇帖子: Python:通过远程监控用户输入来获取淘宝账号和密码的实验(一) 下篇帖子: Python:语音处理,实现在线朗读RFC文档或本地文本文件
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表