可迭代的:對象下有__iter__方法的都是可迭代的對象 迭代器:對象.__iter__()獲得的結果就是迭代器 迭代器的特性: 迭代器.__next__() 取下一個值 優勢: 1.提供了一種統一的迭代對象的方式,不依賴於索引 2.惰性計算 缺點: 1.沒法獲取迭代器的長度 2.一次性的,只能日後取值,不能往前退,不能像索引那樣去取得某個位置的值 生成器:函數內帶有yield關鍵字,那麼這個函數執行的結果就是生成器 生成器本質就是迭代器 def func(): n=0 while True: yield n n+=1 g=func() res=next(g) res=next(g) for i in g: pass 總結yield的功能: 1.至關於把__iter__和__next__方法封裝到函數內部 2.與return比,return只能返回一次,而yield能返回屢次 3.函數暫停已經繼續運行的狀態是經過yield保存的 yield的表達式形式: food=yield def eater(name): print('%s start to eat' %name) while True: food=yield print('%s eat %s' %(name,food)) e=eater('zhejiangF4') #e.send與next(e)的區別 #1.若是函數內yield是表達式形式,那麼必須先next(e) #2.兩者的共同之處是均可以讓函數在上次暫停的位置繼續運行,不同的地方在於 send在觸發下一次代碼的執行時,會順便給yield傳一個值 #協程函數的定義? #協程函數的應用?
定義一個簡單的裝飾器,完成,協和函數先next這個動做、
def init(func): def wrapper(*args,**kwargs): res=func(*args,**kwargs) next(res) return res return wrapper @init #eater=init(eater) def eater(name): print('%s start to eat' % name) food_list=[] while True: food = yield food_list print('%s eat %s' % (name, food)) food_list.append(food) e = eater('zhejiangF4') #wrapper('zhengjiangF4') # print(e) # next(e) #e.send(None) print(e.send('123')) print(e.send('123')) print(e.send('123')) print(e.send('123')) print(e.send('123'))
寫個函數,源源不斷往外,爬網頁
from urllib.request import urlopen def get(): while True: url=yield res=urlopen(url).read() print(res) g=get() next(g) g.send('http://www.python.org')
一個功能,遞歸,查找,文 件夾下的文件python
面向過程,編程思想例題linux
#grep -rl 'python' C:\egon import os,time def init(func): def wrapper(*args,**kwargs): res=func(*args,**kwargs) next(res) return res return wrapper #找到一個絕對路徑,往下一個階段發一個 @init def search(target): '找到文件的絕對路徑' while True: dir_name=yield #dir_name='C:\\egon' print('車間search開始生產產品:文件的絕對路徑') time.sleep(2) g = os.walk(dir_name) for i in g: # print(i) for j in i[-1]: file_path = '%s\\%s' % (i[0], j) target.send(file_path) @init def opener(target): '打開文件,獲取文件句柄' while True: file_path=yield print('車間opener開始生產產品:文件句柄') time.sleep(2) with open(file_path) as f: target.send((file_path,f)) @init def cat(target): '讀取文件內容' while True: file_path,f=yield print('車間cat開始生產產品:文件的一行內容') time.sleep(2) for line in f: target.send((file_path,line)) @init def grep(pattern,target): '過濾一行內容中有無python' while True: file_path,line=yield print('車間grep開始生產產品:包含python這一行內容的文件路徑') time.sleep(0.2) if pattern in line: target.send(file_path) @init def printer(): '打印文件路徑' while True: file_path=yield print('車間printer開始生產產品:獲得最終的產品') time.sleep(2) print(file_path) g=search(opener(cat(grep('python',printer())))) g.send('C:\\egon') g.send('D:\\dir1') g.send('E:\\dir2') #面向過程的編程思想:流水線式的編程思想,在設計程序時,須要把整個流程設計出來 #優勢: #1:體系結構更加清晰 #2:簡化程序的複雜度 #缺點: #1:可擴展性極其的差,因此說面向過程的應用場景是:不須要常常變化的軟件,如:linux內核,httpd,git等軟件
列表生成式git
面試常常會問到面試
#三元表達式 # name='alex' # name='egon' # # res='SB' if name == 'alex' else 'shuai' # print(res) # egg_list=[] # # for i in range(100): # egg_list.append('egg%s' %i) # print(egg_list) # #先進行for循環,而後,再去判斷if子句,而後,再去執行%i把值打印出來 # l=['egg%s' %i for i in range(100) if i > 50] # print(l) l=[1,2,3,4] s='hello' #把列表l中的每一個數字分別與下字符串中的每一個字母拼成元組,拼一遍,如(1,'h')(1,'e'),也能夠不加if判斷,默認全是真 # l1=[(num,s1) for num in l if num > 2 for s1 in s] # print(l1) # l1=[] # for num in l: # for s1 in s: # t=(num,s1) # l1.append(t) # print(l1) 把下面的,列出文件夾下的文件和列表,進行,列表生成式的改進,列表生成式,自己就是append往裏面 # import os # g=os.walk('C:\\egon') # file_path_list=[] # for i in g: # # print(i) # for j in i[-1]: # file_path_list.append('%s\\%s' %(i[0],j)) # # print(file_path_list) # # g=os.walk('C:\\egon') # l1=['%s\\%s' %(i[0],j) for i in g for j in i[-1]] # print(l1)
模擬數據庫查詢數據庫
# # l=['egg%s' %i for i in range(100)] # print(l) 若是用列表生成式,列表是保存在內存中的若是數據太大,直接 死掉,因此這時候就要用到生成器,表達式 也很簡單,直接用()括起來就行,而後,再每次next,或者用for來進行迭代,可是要注意,生成器,也即迭代器, 是一次性的,再用,還要再生成 # g=l=('egg%s' %i for i in range(1000000000000000000000000000000000000)) # print(g) # print(next(g)) # print(next(g)) # for i in g: # print(i) # f=open('a.txt'),a這個文件,每一行兩邊都有空格,須要把空格 去掉 # l=[] # for line in f: # line=line.strip() # l.append(line) # # print(l) # f.seek(0) # l1=[line.strip() for line in f] 列表生成式是中括號,一次全放內存,不推薦, # print(l1) # # f.seek(0) 此鼠標,要返回首頁,很好的解釋了,迭代器,是一次性的 # g=(line.strip() for line in f) 生成器表達式,惰性的,用一個取一個,不用擔憂卡死內存 # print(g) # print(next(g)) # f=open('a.txt') # g=(line.strip() for line in f) # l=list(g) # print(l) sum這個函數,()中傳的只要是個可迭代的對像,他像for同樣,一次next一個,把你傳進去的變量, 最後求和 # nums_g=(i for i in range(3)) # # # print(sum([1,2,3,4])) # print(sum(nums_g)) 下圖中的b.txt相似於等,把文件裏面的值求和 mac 10000 1 電腦 5000 1 # money_l=[] # with open('b.txt') as f: # for line in f: # goods=line.split() # res=float(goods[-1])*float(goods[-2]) # money_l.append(res) # print(money_l) #下面是用聲明式編程,進行的求和 # f=open('b.txt') # g=(float(line.split()[-1])*float(line.split()[-2]) for line in f) # # print(sum(g)) # with open('b.txt') as f: # print(sum((float(line.split()[-1])*float(line.split()[-2]) for line in f))) # res=[] # with open('b.txt') as f: # for line in f: # # print(line) # l=line.split() # # print(l) # d={} # d['name']=l[0] # d['price']=l[1] # d['count']=l[2] # res.append(d) # print(res) # with open('b.txt') as f: # res=(line.split() for line in f) # print(res) # dic_g=({'name':i[0],'price':i[1],'count':i[2]} for i in res) # print(dic_g) # apple_dic=next(dic_g) # print(apple_dic['count']) # apple_dict=next(dic_g) # print(apple_dict) #取出單價>10000 with open('b.txt') as f: res=(line.split() for line in f) # print(res) dic_g=({'name':i[0],'price':i[1],'count':i[2]} for i in res if float(i[1]) > 10000) print(dic_g) print(list(dic_g)) # for i in dic_g: # print(i)