從上學時開始,通常是用C來求階乘,今天無事,用python寫了一下,主要在于學習lambda和reduce這兩個函數的使用。
實現:
~~~
#!/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()
~~~
測試:
~~~
**************************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
手冊中這樣描述:
~~~
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 * y
print reduce(my_func, range(1, long(raw_input("please input a number ( > 0):"))))
~~~
運行結果:
~~~
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
~~~
- 前言
- Python:實現文件歸檔
- Pyhon:按行輸出文件內容
- Python:讀文件和寫文件
- Python:實現一個小算法
- Python:通過命令行發送新浪微博
- Python:通過攝像頭實現的監控功能
- Python:通過攝像頭抓取圖像并自動上傳至新浪微博
- Python:簡單的攝像頭程序實現
- Python:日志模塊logging的應用
- Python:操作嵌入式數據庫SQLite
- Python:將句子中的單詞全部倒排過來,但單詞的字母順序不變
- Python:語音處理,實現在線朗讀RFC文檔或本地文本文件
- Python:通過計算階乘來學習lambda和reduce這兩個函數的使用
- Python:通過執行100萬次打印來比較C和python的性能,以及用C和python結合來解決性能問題的方法
- Python:使用matplotlib繪制圖表
- Python:使用pycha快速繪制辦公常用圖(餅圖、垂直直方圖、水平直方圖、散點圖等七種圖形)
- Python:使用pycha快速繪制辦公常用圖二(使用樣式定制個性化圖表)
- Python:監控鍵盤輸入、鼠標操作,并將捕獲到的信息記錄到文件中
- Python:通過獲取淘寶賬號和密碼的實驗,來看登陸方式選擇的重要性
- Python:通過獲取淘寶賬號和密碼的實驗,來看登陸方式選擇的重要性(二)
- Python:通過遠程監控用戶輸入來獲取淘寶賬號和密碼的實驗(一)
- Python:通過遠程監控用戶輸入來獲取淘寶賬號和密碼的實驗(二)
- Python:通過自定義系統級快捷鍵來控制程序運行
- Python:通過自定義系統級快捷鍵來控制程序開始或停止記錄日志(使用小技巧解決一個貌似無解的問題)
- Python:一個多功能的抓圖工具開發(附源碼)
- Python:程序發布方式簡介一(打包為可執行文件EXE)
- Python:新浪微博應用開發簡介(認證及授權部分)
- Python:程序最小化到托盤功能實現
- Python:實用抓圖工具開發介紹(含需求分析、設計、編碼、單元測試、打包、系統測試、發布各環節)
- Python:桌面氣泡提示功能實現
- Python:未來三個月的python學習計劃
- Python:pygame模塊及SDL庫簡介
- Python:獲取新浪微博用戶的收聽列表和粉絲列表
- Python:pygame游戲編程之旅一(Hello World)
- Python:pygame游戲編程之旅二(自由移動的小球)
- Python:pygame游戲編程之旅三(玩家控制的小球)
- Python:pygame游戲編程之旅四(游戲界面文字處理)
- Python:pygame游戲編程之旅五(游戲界面文字處理詳解)
- Python:pygame游戲編程之旅六(游戲中的聲音處理)
- Python:pygame游戲編程之旅七(pygame基礎知識講解1)
- Python:編程“八榮八恥”之我見
- Python:腳本的幾種執行方式
- wxPython:簡單的wxPython程序
- wxPython:簡單的wxPython程序的另一種寫法
- wxPython:應用程序對象介紹
- wxPython:輸出重定向
- wxPython:關閉wxPython程序
- wxPython:Frame類介紹
- wxPython:面板Panel的使用
- wxPython:工具欄、狀態欄、菜單實現
- wxPython:消息對話框MessageDialog
- wxPython:文本對話框TextEntryDialog
- wxPython:列表選擇框SingleChoiceDialog
- wxPython:事件處理介紹一
- wxPython:事件處理介紹二
- wxPython: 簡單的繪圖例子
- wxPython:狀態欄介紹
- wxPython:菜單介紹
- wxPython:文件對話框wx.FileDialog
- wxPython:顏色選擇對話框wx.ColourDialog
- wxPython:布局管理器sizer介紹
- wxPython:啟動畫面SplashScreen介紹
- wxPython:繪畫按鈕BitmapButton介紹
- wxPython:進度條Gauge介紹
- Python: 發送新浪微博(使用oauth2)
- Python:讀取新浪微博收聽列表
- Python:DNS客戶端實現