標籤:python基礎 面向過程
面向過程編程
核心是過程二字,過程即解決問題的步驟,基於面向過程去設計程式就像是在設計一條工業流水線,是一種機械式的思維方式
優點:程式結構清晰可以把複雜的問題簡單化,流程化
缺點:可擴充性差,一條流線只是用來解決一個問題
應用情境:linux核心,git,httpd,shell指令碼
練習:過濾目錄下檔案內容包含error的檔案
grep –rl ‘error’ /dir
使用os模組walk方法:
os.walk會把目錄下的二級目錄和檔案做成一個迭代器,多次使用實現檔案路徑的拼接
650) this.width=650;" src="https://s2.51cto.com/wyfs02/M02/9D/92/wKiom1mCAsCj8FMCAAFWif5hlXI802.png" title="QQ圖片20170803004928.png" alt="wKiom1mCAsCj8FMCAAFWif5hlXI802.png" />
#grep -rl ‘error‘ /dir/import osdef init(func): def wrapper(*args,**kwargs): g=func(*args,**kwargs) next(g) return g return wrapper#第一階段:找到所有檔案的絕對路徑@initdef search(target): while True: filepath=yield g=os.walk(filepath) for pardir,_,files in g: for file in files: abspath=r‘%s\%s‘ %(pardir,file) target.send(abspath)#第二階段:開啟檔案@initdef opener(target): while True: abspath=yield with open(abspath,‘rb‘) as f: target.send((abspath,f))#第三階段:迴圈讀出每一行內容@initdef cat(target): while True: abspath,f=yield #(abspath,f) for line in f: res=target.send((abspath,line)) if res:break#第四階段:過濾@initdef grep(pattern,target): tag=False while True: abspath,line=yield tag tag=False if pattern in line: target.send(abspath) tag=True#第五階段:列印該行屬於的檔案名稱@initdef printer(): while True: abspath=yield print(abspath)g = search(opener(cat(grep(‘error‘.encode(‘utf-8‘), printer()))))g.send(r‘D:\python location\python36\day05\a‘)
3、遞迴
遞迴調用:在調用一個函數的過程中,直接或間接地調用了函數本身
Python中的遞迴在進行下一次遞迴時必須要儲存狀態,效率低,沒有最佳化手段,所以對遞迴層級做了限制(其他程式設計語言中有尾遞迴方式進行最佳化)
1. 必須有一個明確的結束條件
2. 每次進入更深一層遞迴時,問題規模相比上次遞迴都應有所減少
3. 遞迴效率不高,遞迴層次過多會導致棧溢出(在電腦中,函數調用是通過棧(stack)這種資料結構實現的,每當進入一個函數調用,棧就會加一層棧幀,每當函數返回,棧就會減一層棧幀。由於棧的大小不是無限的,所以,遞迴調用的次數過多,會導致棧溢出)
尾遞迴最佳化:http://egon09.blog.51cto.com/9161406/1842475
#直接def func(): print(‘from func‘) func()func() 輸出:from funcfrom func…from funcTraceback (most recent call last): File "D:/python location/python36/day05/遞迴.py", line 8, in <module> func() [Previous line repeated 993 more times]RecursionError: maximum recursion depth exceeded while calling a Python object #調用Python對象時的最大遞迴深度超過了限制
如果遞迴層級過多,會報如上錯誤
#間接def foo(): print(‘from foo‘) bar()def bar(): print(‘from bar‘) foo()foo() 輸出:RecursionError: maximum recursion depth exceeded while calling a Python object #調用Python對象時的最大遞迴深度超過了限制
修改遞迴層級限制(預設1000)
>>> import sys>>> sys.getrecursionlimit()1000>>> sys.setrecursionlimit(2000)>>> sys.getrecursionlimit()2000
練習:
已知:
age(5)=age(4)+2
age(4)=age(3)+2
age(3)=age(2)+2
age(2)=age(1)+2
age(1)=18
首先做判斷:
age(n)=age(n-1)+2 #n>1
age(1)=18 #n=1
def age(n): if n == 1: return 18 return age(n-1)+2print(age(5))
遞迴的執行分為兩個階段:
1 遞推
2 回溯
650) this.width=650;" src="https://s5.51cto.com/wyfs02/M02/9D/92/wKioL1mCA-azEeaZAABbE7nPVcM799.png" title="QQ圖片20170803005444.png" alt="wKioL1mCA-azEeaZAABbE7nPVcM799.png" />
遞迴和迴圈功能差不多,但在不知道迴圈次數時適合使用遞迴
練習:
取出列表中所有的元素
l =[1, 2, [3, [4, 5, 6, [7, 8, [9, 10, [11, 12, 13, [14,15,[16,[17,]],19]]]]]]] #def search(l): for item in l: if type(item) is list: search(item) else: print(item)search(l)
4、二分法
方法:
判斷一個數值是否存在於一個特別大的列表中,如果使用in方法會遍曆列表,占記憶體過多,使用二分法每次會平分列表,佔用記憶體較少
練習:
#二分法l = [1,2,5,7,10,31,44,47,56,99,102,130,240]def binary_search(l,num): print(l) #[10, 31] if len(l) > 1: mid_index=len(l)//2 #1 if num > l[mid_index]: #in the right l=l[mid_index:] #l=[31] binary_search(l,num) elif num < l[mid_index]: #in the left l=l[:mid_index] binary_search(l,num) else: print(‘find it‘) else: if l[0] == num: print(‘find it‘) else: print(‘not exist‘) returnbinary_search(l,32)
本文出自 “lyndon” 部落格,請務必保留此出處http://lyndon.blog.51cto.com/11474010/1953169
python基礎---面向過程編程