标签:ons rom 可迭代对象 包子 字典 ret read %s map
#上节课复习 # l=[1,2,3,4,5] # print(list(map(str,l))) #reduce # from functools import reduce # l=[1,2,3,4,5] # #reduce(func,l) # res=reduce(lambda x,y:x+y,l,3) # print(res) #filter() # name=[‘alex_sb‘,‘zzc‘] # res=filter(lambda x:not x.endswith(‘sb‘),name) # print(list(res)) # 文件操作 # f=open(‘test.py‘,‘w‘,encoding=‘utf8‘) # # f.write(‘1111\n‘) # # f.write(‘1111\n‘) # # f.write(‘1111\n‘) # f.write(‘amoshi‘) # # f.close() #r+ w+ a+ # f=open(‘test.py‘,‘r+‘,encoding=‘utf8‘) # f.write(‘hallo‘) #覆盖第一行 #b模式 # f=open(‘test.py‘,‘rb‘,encoding=‘utf8‘) #b的方式不能指定编码 # f=open(‘test.py‘,‘rb‘) # data=f.read() # #‘字符串‘--encode----->bytes # #‘字符串‘--decode----->‘字符串‘ # # print(data) #b‘# halloi\r\n# afasfasfsf\r\n# fasfsf\r\n\xe4\xbd\xa0\xe5\xa5\xbd\xe5\x95\x8a‘ #print(data.decode(‘utf8‘) # f=open(‘test.py‘,‘wb‘) #b的方式不能指定编码 # # f.write(‘1111\n‘) #错误 # f.write(bytes(‘1111\n‘,encoding=‘utf8‘)) # f.write (‘1111\n‘.encode(‘utf8‘)) # f=open(‘a.txt‘,‘r‘,encoding=‘latin-1‘) # # print(f.encoding) #文件打开的编码 # data=f.read() # print(data) #f=open(‘test.py‘,‘r+‘,encoding=‘utf-8‘,newline=‘‘)#读取文件中真正的换行符号 # print(f.tell()) # print(f.readlines()) # f.seek(5) #控制光标的移动 按字节处理 # print(f.tell()) # print(f.readlines()) # print(f.read()) # 按字符读取 # f.truncate(10) #截取文件 在写文件 # # f=open(‘seek.txt‘,‘rb‘ ) # print(f.tell()) # f.seek(10,0) #从0开始数字节 # print(f.tell()) # f.seek(3,1) #从xiangduiweizhi # print(f.tell()) # f=open(‘seek.txt‘,‘rb‘ ) # print(f.tell()) # f.seek(10,2) #从文件末尾 # print(f.tell()) # f=open(‘日志文件‘,‘rb‘) # data=f.readlines() # print(data[-1].decode(‘utf-8‘)) #f=open(‘日志文件‘,‘rb‘) # for i in f.readlines(): # print(i) #循环文件的方式 # for i in f: # print(i.decode(‘utf8‘)) # for i in f: # offs=-10 # while True: # f.seek(offs,2) # data=f.readlines() # if len(data)>1: # print(‘文件的最后一行%s‘ %(data[-1].decode(‘utf8‘))) # break # offs*=2 #迭代器和生成器 # 迭代器协议:对象必须提供一个next方法,执行该方法要么返回迭代中的下一项,要么就引起一个StopLteration异常,以终止迭代(只能往后走不能往前退) # 可迭代对象:实现了迭代器协议的对象(如何实现:对象内部定义一个__iter__()方法) # 协议是一种约定,可迭代对象实现了迭代器协议,python内部工具(如 for循环 sum min max) # l=[1,2,3] # #for i in l: #i_l=l.__iter__() i_l.__next__() # print(l[0]) # # iter_l=l.__iter__() #生成可迭代对象 # print(iter_l.__next__()) # index=0 # while index<len(l): # print(l[index]) # index+=1 # x=‘csf‘ # s=x.__iter__() # print(s) # print(s.__next__()) # print(s.__next__()) # print(s.__next__()) # s={1,2,3} # # # for i in s: # # print(i) # iter_s=s.__iter__() # print(iter_s) # print(iter_s.__next__()) # dic={‘a‘:1,‘b‘:2,‘c‘:3} # iter_d=dic.__iter__() # print(iter_d.__next__()) # l=[‘die‘,‘erzi‘,‘sunzi‘,‘chongsunzi‘] # # iter_l=l.__iter__() # print(iter_l) #可迭代对象 # print(iter_l.__next__()) # print(iter_l.__next__()) # print(iter_l.__next__()) # print(iter_l.__next__()) # print(next(iter_l)) #调用iter_l.__next__() #生成器 # x= # x.__next__() # def test(): # yield 1 相当于__iter__() # yield 2 # yield 3 # # g=test() # print(g) # g.__next__() #三元表达式 # name=‘zzc‘ # res=‘haha‘ if name==‘zzc‘ else ‘csf‘ # print(res) #列表解析 # egg_list=[] # for i in range(10): # egg_list.append(‘鸡蛋%s‘,%i) # print(egg_list) # l=[ ‘鸡蛋%s‘ %i for i in range(10)] # l2=[‘鸡蛋%s‘ %i for i in range(10) if i>5] # print(l) # laomuji=(‘鸡蛋%s‘ %i for i in range(10 ))#生成器表达式 # print(laomuji) # print(laomuji.__next__()) # l=[1,2,3,4,5,6] # print(sum(i for i in range(1000000000000)))#生成器表达式 # import time # def test(): # print(‘haha‘) # print(‘haha2‘) # print(‘haha3‘) # yield ‘wo‘ #return # time.sleep(3) # print(‘haha4‘) # yield ‘gg‘ # # res=test() # print(res) # print(res.__next__()) # print(res.__next__()) # def xiadan(): # for i in range(1000): # yield ‘jidan‘ # csf=xiadan() # zzc=csf.__next__() # print(‘qujidan‘,zzc) #eval(i)#转成字典 #生产者消费模型 # import time # def producer(): # ret=[] # for i in range(10000): # ret.append(‘包子%s‘ %i) # return ret # def consumer(res): # for index,baozi in enumerate(res): # time.sleep(0.1) # print(‘第%s个人,吃了%s‘ %(index,baozi)) # # res=producer() # consumer(res) # def test(): # print(‘开始啦‘) # first=yield 1 #retuen1 first=None # print(‘第一次‘,first) # yield 2 # print(‘第二次‘) # # t=test() # res=t.__next__() #next(t) # print(res) # t.send(None) # def producer(): # ret=[] # for i in range(10000): # ret.append(‘包子%s‘ %i) # return ret # def consumer(name): # print(‘我是%s,我要吃包子了‘ %name) # while True: # baozi=yield #yield相当于return # print(‘%s 很开心的吧%s吃了‘ %(name,baozi)) # def producer(): # c1 = consumer(‘陈淑芳‘) # c2 = consumer(‘张志成‘) # c1.__next__() # c2.__next__() # for i in range(10): # time.sleep(1) # c1.send(‘包子‘) # c2.send(‘baozi‘) # producer() # for index,baozi in enumerate(res): # time.sleep(0.1) # print(‘第%s个人,吃了%s‘ %(index,baozi)) # # res=producer() # consumer(res)
标签:ons rom 可迭代对象 包子 字典 ret read %s map
原文地址:https://www.cnblogs.com/zzcasf/p/9949919.html