1. 程式人生 > >可愛的python課後習題(二)

可愛的python課後習題(二)

1,結合grep的例項,考慮處理子目錄的情況:

#!/usr/bin/python
#coding=utf-8
#filename:cdcGrep.py
import os
def cdcGreper(cdcpath,keyword):
        filelist=os.listdir(cdcpath)
        for cdc in filelist:
                if os.path.isdir(cdc):
                        filename=cdcpath+os.sep+cdc
                        print '%s 是子目錄 ' % filename
                        cdcGreper(filename,keyword)
                        print '執行迭代函式'
                elif '.txt' in cdc:
                        print '找到目標檔案,準備讀取'
                        cdcfile=open(cdcpath+os.sep+cdc)
                        for line in cdcfile.readlines():
                                if keyword in line:
                                        print line
if __name__=='__main__':
        cdc=cdcGreper('/home/zhouqian/python','test')
結果顯示:
[email protected]:~/python$ python cdcGrep.py
找到目標檔案,準備讀取
./ ['class'] ['getoptTest.py', 'value_keys.py', 'text.txt', 'cdctool.py', 'test.txt', 'cdctoolTest.py', 'cdWalk.py', '.getoptTest.py.swp', 'cdWalk.pyc']

/home/zhouqian/python/class 是子目錄 
找到目標檔案,準備讀取
test

test

test

執行迭代函式
找到目標檔案,準備讀取
test
說明下這裡面友好多的print,是為了方便除錯最笨的方法。
遇到問題總結:
os.sep是一個分割符的標誌,
os.path.isdir是驗證是否存在子目錄的函式
迭代的巧妙運用,
in的使用:
>>> a='12345'
>>> 1 in a
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'in <string>' requires string as left operand, not int
>>> '1' in a
True
>>> a=(1,2,3,4)
>>> 1 in a
True
>>> a=['1','2','3']
>>> 1 in a
False
>>> '1' in a
True
>>> a={1:11,2:33}
>>> 1 in a
True

習題2:編寫類實現棧的功能----FILO:

#!/usr/bin/python
#coding=utf-8
#filename:MyStack.py
class MyStacker(object):
        '''
           mystack 自定義棧,主要的操作put(),get(),isEmpty()
        '''
        def __init__(self,max):
                '''初始化棧頭指標和清空棧'''
                self.head=-1
                self.max=max
                self.stack=list()#這裡使用list列表來儲存資料
                for i in range(self.max):
                        self.stack.append(0)#這裡是初始化stack的長度,也就是分配儲存空間
        def put(self,item):
                #首先判斷是否超出了棧的長度
                if self.head>=self.max:
                        return '棧已滿,請先刪除部分資料'
                else:
                        self.head+=1
                        self.stack[self.head]=item
                        print 'put %s successfully' %item
        def get(self):
                print '進入get函式中'
                if self.head<0:
                        return '棧已空,請先插入資料'
                else:
                        print '判斷通過'
                        self.head-=1
                        print self.head
                        return self.stack[self.head+1]#此處這樣寫的目的是為了能夠取到我們需要的那個資料,並且self.head還要減1
        def isEmpty(self):
                if self.head<-1:
                        print '該棧為空'
                        return True
                else:
                        print '該棧不為空,資料為%s' %self.stack
                        return False
if __name__=='__main__':
        mystack=MyStacker(10)
        mystack.put('a')
        mystack.put('chen')
        mystack.put('zhou')
        print mystack.get()
        print '##########'
        mystack.isEmpty()
        print mystack.get()

結果顯示:
[email protected]:~/python$ python MyStack.py 
put a successfully
put chen successfully
put zhou successfully
進入get函式中
判斷通過
1
zhou
##########
該棧不為空,資料為['a', 'chen', 'zhou', 0, 0, 0, 0, 0, 0, 0]
進入get函式中
判斷通過
0
chen
遇到的問題總結:

實現FILO時選擇資料結構:我們這裡選擇列表,主要是它能刪能插,剛開始想用tuple的,可是它的資料在定義完後就沒法在修改了。所以只能使用list來做

list的基本操作,檢索的使用List 可以作為以 0 下標開始的陣列。任何一個非空 list 的第一個元素總是 li[0]

python中類的使用注意事項。