迭代的工具
指的是一個重複的過程,每一次重複稱爲一次迭代,而且每一次重複的結果是下一次重複的初始值
while True: print('=====>')
l=['a','b','c'] count=0 while count < len(l): print(l[count]) count+=1
對於序列類型:str,list,tuple,能夠依賴索引來迭代取值,
可是對於dict,set,文件,python必須爲咱們提供一種不依賴於索引的迭代取值的方式-》迭代器
name='egon' l=[1,2,3] t=(1,2,3) d={'name':'egon','age':18,'sex':'male'} s={'a','b','c'} f=open('a.txt','w',encoding='utf-8')
name.__iter__ l.__iter__ t.__iter__ d.__iter__ s.__iter__ f.__iter__
f.__iter__ f.__next__
1 可迭代對象不必定是迭代器對象
2 迭代器對象必定是可迭代的對象
3 調用obj.__iter__()方法,獲得的是迭代器對象(對於迭代器對象,執行__iter__獲得的仍然是它自己)
d={'name':'egon','age':18,'sex':'male'} d_iter=d.__iter__()
f=open('a.txt','w',encoding='utf-8') f_iter=f.__iter__().__iter__().__iter__().__iter__() print(f_iter is f)
d={'name':'egon','age':18,'sex':'male'} d_iter=d.__iter__() print(d_iter.__next__()) print(d_iter.__next__()) print(d_iter.__next__()) print(d_iter.__next__())
迭代器d_iter沒有值了,就會拋出異常StopIterationpython
f=open('a.txt','r',encoding='utf-8') print(f.__next__()) print(f.__next__()) print(f.__next__()) print(f.__next__()) f.close()
l=['a','b','c'] l_iter=l.__iter__() print(l_iter.__next__()) print(l_iter.__next__()) print(l_iter.__next__()) print(l_iter.__next__())
d={'name':'egon','age':18,'sex':'male'} d_iter=iter(d) #d_iter=d.__iter__()
len(obj) 等同於obj.__len__()
while True: try: print(next(d_iter)) #print(d_iter.__next__()) except StopIteration: break print('=>>>') print('=>>>') print('=>>>') print('=>>>')
for循環詳解:編程
一、調用in後的obj_iter=obj.__iter__()
二、k=obj_iter.__next__()
三、捕捉StopIteration異常,結束迭代
d={'name':'egon','age':18,'sex':'male'} for k in d: print(k)
一、提供一種統一的、不依賴於索引的取值方式,爲for循環的實現提供了依據
二、迭代器同一時間在內存中只有一個值——》更節省內存,
一、只能日後取,而且是一次性的
二、不能統計值的個數,即長度
l=[1,2,3,4,5,6] l[0] l[1] l[2] l[0] l_iter=l.__iter__() # print(l_iter) print(next(l_iter)) print(next(l_iter)) print(next(l_iter)) print(next(l_iter)) print(next(l_iter)) print(next(l_iter)) print(next(l_iter))
l_iter=l.__iter__() print(next(l_iter)) print(next(l_iter)) print(next(l_iter)) print(len(l_iter))
只要在函數體內出現yield關鍵字,那麼再執行函數就不會執行函數代碼,會獲得一個結果,該結果就是生成器app
def func(): print('=====>1') yield 1 print('=====>2') yield 2 print('=====>3') yield 3 生成器就是迭代器 g=func() res1=next(g) print(res1) res2=next(g) print(res2) res3=next(g) # print(res3)
一、yield爲咱們提供了一種自定義迭代器對象的方法
二、yield與return的區別1:yield能夠返回屢次值 #2:函數暫停與再繼續的狀態是由yield幫咱們保存的
obj=range(1,1000000000000000000000000000000000000000000000000000000000000000,2) obj_iter=obj.__iter__() print(next(obj_iter)) print(next(obj_iter)) print(next(obj_iter)) print(next(obj_iter)) print(next(obj_iter))
def my_range(start,stop,step=1): while start < stop: yield start #start=1 start+=step #start=3 g=my_range(1,5,2) print(g) print(next(g)) print(next(g)) print(next(g)) print(next(g)) print(next(g)) print(next(g)) print(next(g)) for i in my_range(1,5,2): print(i)
import time def tail(filepath): with open(filepath,'rb') as f: f.seek(0,2) while True: line=f.readline() if line: yield line else: time.sleep(0.05) def grep(lines,pattern): for line in lines: line=line.decode('utf-8') if pattern in line: yield line lines=grep(tail('access.log'),'404') for line in lines: print(line)
def eater(name): print('%s ready to eat' %name) food_list=[] while True: food=yield food_list#food=yield='一盆骨頭' food_list.append(food) print('%s start to eat %s' %(name,food)) e=eater('alex') #首先初始化: print(e.send(None)) # next(e) #而後e.send:1 從暫停的位置將值傳給yield 二、與next同樣 print(e.send('一桶泔水')) print(e.send('一盆骨頭'))
with open('access.log','a',encoding='utf-8') as f: f.write('bbbbb 404\n') f.flush()
grep -rl 'python' /etc
補充:os.walk
import os g=os.walk(r'D:\video\python20期\day4\a') # print(next(g)) # print(next(g)) # print(next(g)) # print(next(g)) for pardir,_,files in g: for file in files: abs_path=r'%s\\%s' %(pardir,file) print(abs_path)
import os def search(target): #r'D:\video\python20期\day4\a' while True: filepath=yield #fllepath=yield=r'D:\video\python20期\day4\a' g=os.walk(filepath) for pardir, _, files in g: for file in files: abs_path = r'%s\%s' % (pardir, file) # print(abs_path) target.send(abs_path) search(r'D:\video\python20期\day4\a') search(r'D:\video\python20期\day4')
def opener(): while True: abs_path=yield print('opener func--->',abs_path) target=opener() next(target) #target.send('xxxx') g=search(target) next(g) g.send(r'D:\video\python20期\day4\a')
import os def init(func): def inner(*args,**kwargs): g=func(*args,**kwargs) next(g) return g return inner @init def search(target): # r'D:\video\python20期\day4\a' while True: filepath = yield g = os.walk(filepath) for pardir, _, files in g: for file in files: abs_path = r'%s\%s' % (pardir, file) #把abs_path傳給下一個階段 target.send(abs_path)
@init def opener(target): while True: abs_path = yield with open(abs_path,'rb') as f: #把(abs_path,f)傳給下一個階段 target.send((abs_path,f))
@init def cat(target): while True: abs_path,f=yield for line in f: #把(abs_path,line)傳給下一個階段 res=target.send((abs_path,line)) #知足某種條件,break掉for循環 if res: break
@init def grep(target,pattern): pattern = pattern.encode('utf-8') res=False while True: abs_path,line=yield res res=False if pattern in line: #把abs_path傳給下一個階段 res=True target.send(abs_path)
@init def printer(): while True: abs_path=yield print('<%s>' %abs_path) g=search(opener(cat(grep(printer(),'python')))) #'python' in b'xxxxx' g.send(r'D:\video\python20期\day4\a')
面向過程編程:核心是過程二字,過程指的就是解決問題的步驟,即先幹什麼後幹什麼,基於該思路編寫程序就比如設計一條流水線,是一種機械式的思惟方式
優勢:
複雜的問題流程化、進而簡單化
缺點:
可擴展性差
def my_max(x,y): if x >= y: return x else: return y x=10 y=20 # res=x if x >= y else y # print(res) name=input('>>: ').strip() res='Sb' if name == 'alex' else 'NB' print(res)
l=[] for i in range(1,11): res='egg'+str(i) l.append(res) print(l) l=['egg'+str(i) for i in range(1,11)] print(l) l1=['egg'+str(i) for i in range(1,11) if i >= 6] print(l1) l1=[] for i in range(1,11): if i >= 6: l1.append('egg'+str(i))
g=('egg'+str(i) for i in range(0,1000000000000000000000000000000000)) print(g) print(next(g)) print(next(g)) print(next(g))
names=['egon','alex_sb','wupeiqi','yuanhao'] names=[name.upper() for name in names] print(names) sbs=[name for name in names if name.endswith('sb')] print(sbs) obj=list('abcdef') print(obj) print(max([1,2,3,4,5])) g=(i for i in range(10)) print(max(g)) print(max(g)) with open('a.txt','r',encoding='utf-8') as f: l=[] for line in f: # print(len(line)) l.append(len(line)) g=(len(line) for line in f) res=max(g) print(res) print(max(len(line) for line in f)) print(sum(len(line) for line in f))
在調用一個函數的過程當中,直接或者間接又調用該函數自己,稱之爲遞歸調用
一、遞推ide
二、回溯函數
import sys print(sys.getrecursionlimit()) sys.setrecursionlimit(2000) print(sys.getrecursionlimit()) def func(n): print('---->',n) func(n+1) func(0) def bar(): print('from bar') func() def func(): print('from func') bar() func() 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) + 2 res=age(5) print(res) l=[1,[2,[3,[4,[5,[6,[7,]]]]]]] def func(l): for item in l: if type(item) is list: func(item) else: print(item) def func(): print('===>') func() func()
l=[1,2,10,30,33,99,101,200,301,402] #從小到大排列的數字列表 def binary_search(l,num): print(l) if len(l) == 0: print('not exists') return mid_index=len(l) // 2 if num > l[mid_index]: #往右找 binary_search(l[mid_index+1:],num) elif num < l[mid_index]: #往左找 binary_search(l[0:mid_index],num) else: print('find it') # binary_search(l,301) binary_search(l,302)
def func(): #func=內存地址 print('from func') func() func()
def my_sum(x,y): return x+y
print(lambda x,y:x+y) print((lambda x,y:x+y)(1,2))
func=lambda x,y:x+y # print(func) print(func(1,2))
max,min,sorted,map,reduce,filter工具
salaries={ 'egon':3000, 'alex':100000000, 'wupeiqi':10000, 'yuanhao':2000 } print(max(salaries))
s='hello' l=[1,2,3] g=zip(s,l) # print(g) print(list(g))
g=zip(salaries.values(),salaries.keys()) # print(list(g)) print(max(g)) def func(k): return salaries[k] print(max(salaries,key=func)) #key=func('egon') print(max(salaries,key=lambda k:salaries[k])) #key=func('egon') print(min(salaries,key=lambda k:salaries[k])) #key=func('egon')
sorted
salaries={ 'egon':3000, 'alex':100000000, 'wupeiqi':10000, 'yuanhao':2000 } print(sorted(salaries,key=lambda k:salaries[k])) print(sorted(salaries,key=lambda k:salaries[k],reverse=True))
map,reduce,filter
names=['alex','wupeiqi','yuanhao'] l=[] for name in names: res='%s_SB' %name l.append(res) print(l) g=map(lambda name:'%s_SB' %name,names) # print(g) print(list(g))
names=['alex_sb','wupeiqi_sb','yuanhao_sb','egon'] g=filter(lambda x:x.endswith('sb'),names) print(g) print(list(g))
from functools import reduce print(reduce(lambda x,y:x+y,range(1,101),100))
print(abs(-1))
print(all([1,'a','b',0])) print(all([]))
print(any([None,False,0,1])) print(any([]))
print(bin(11)) print(hex(11)) print(oct(11))
print('xxx'.encode('utf-8')) print(bytes('xxx',encoding='utf-8'))
print(callable(max))
print(chr(65)) # print(chr(90)) # print(chr(39)) print(ord('A')) print(ord('@'))
import os print(dir(os)) s=set({1,2,3}) s.add(4) print(s)
s=frozenset({1,2,3}) #不可變集合 print(hash('xxx'))
l=[1,2,'a',4] print(list(reversed(l)))
s=slice(1,5,2) l=['a','b','c','d','e']
# print(l[1:5:2]) # print(l[1:5:2]) print(l[s])
print(vars() is locals())
obj.__dict__() #vars(obj)
classmethod
staticmethod
property
hasattr
getattr
setattr
delattr
isinstance
issubclass
object
super
import
__import__ choice=input('>>: ') print(choice,type(choice)) # import 'time' m=__import__(choice) m.sleep(10)
# print(divmod(10011,25))
l=['a','b','c'] for i in l: print(l.index(i),i,) for i,v in enumerate(l): print(i,v)
res=eval('[1,2,3]') print(res,type(res)) res=exec('[1,2,3]') print(res)
res=pow(2,3,3) # (2 ** 3 )%3 print(res)
print(round(3.5))