Python記錄12:迭代器+生成器+生成式
阿新 • • 發佈:2018-12-05
'''
1. 什麼是迭代器
什麼是迭代:迭代就是一個重複的過程,但是每一次重複都是基於上一次的結果而進行的
單純的重複不是迭代:
while True:
print(1)
迭代的過程
l=['a','b','c']
i=0
while i < len(l):
print(l[i])
i+=1
迭代器:迭代取值的工具
2. 為何要用迭代器
迭代器的優點:
1. 提供了一種可以不依賴索引的迭代取值方式
3. 如何用迭代器
'''
# 可迭代的物件:但凡內建有__iter__方法的物件就是可迭代的物件,例如:str\list\tuple\dict\set\檔案物件
# ''.__iter__()
# [].__iter__()
# (1,2).__iter__()
# {'x':1}.__iter__()
# {1,2,3}.__iter__()
# open('今日內容').__iter__()
# 呼叫可迭代物件的__iter__()方法,會得到一個返回值,該返回值就是迭代器物件
# 迭代器物件: 既內建有__iter__方法又內建有__next__方法,例如檔案物件
# dic={'x':1,'y':2,'z':3}
# print(len(dic)) #dic.__len__()
# iter_dic=dic.__iter__()
# # print(iter_dic)
# print(iter_dic.__next__())
# print(iter_dic.__next__())
# print(iter_dic.__next__())
# # print(iter_dic.__next__())
#
# new_iter_dic=dic.__iter__()
# print(new_iter_dic.__next__())
# print(new_iter_dic.__next__())
# print(new_iter_dic.__next__())
# iter_dic=iter(dic) #dic.__iter__()
# next(iter_dic) #iter_dic.__next__()
# 迭代器總結:
# 優點:
# 1. 提供了一種不依賴索引的迭代器取值方式
# 2. 節省記憶體
# 缺點:
# 1. 一次性,只能往後一直取,無法預測迭代中包含的值的個數
# 2. 無法取到指定的值,不如按照索引或者key的取值方式靈活
dic={'x':1,'y':2,'z':3}
# iter_dic=iter(dic)
#
# while True:
# try:
# print(next(iter_dic))
# except StopIteration:
# break
# print('='*100)
# # iter_dic=iter(dic)
# while True:
# try:
# print(next(iter_dic))
# except StopIteration:
# break
# for k in dic:
# print(k)
# for迴圈的底層工作原理:
#1. 呼叫in後面那物件的內建方法__iter__,拿到一個迭代器物件iter_obj
#2. 執行k=next(iter_obj),迴圈往復直到丟擲異常StopIterration
#3. for迴圈會捕捉異常然後結束迴圈
dic={'x':1,'y':2,'z':3}
iter_dic=iter(dic)
print(iter_dic.__iter__().__iter__().__iter__() is iter_dic)
# with open(r'D:\週末三期\day06\01 疊加多個裝飾器.py','r',encoding='utf-8') as f:
# for line in f:
# print(line)
'''
1. 什麼是生成器
生成器就是一種自定義的迭代器
3. 如何使用生成器:
在函式內但凡出現yield關鍵字,然後呼叫函式不會立即執行函式體程式碼,會得到一個返回值,該返回值稱之為生成器,即我們自定義的迭代器
'''
# def func():
# print('first')
# yield 1
# print('second')
# yield 2
# print('third')
# yield 3
# print('fourth')
#
# g=func()
# # print(g)
# # g是生成器物件,而生成器物件本質就是迭代器
# res1=next(g)
# print(res1)
#
# res2=next(g)
# print(res2)
#
# res3=next(g)
# print(res3)
#
# next(g)
# def my_range(start,stop,step=1): #start=1 stop=7 step=2
# while start < stop: # 7 < 7
# yield start #
# start+=step # start=7
#
#
# res=my_range(1,70000000000000000000000000000000000000000000000000000000,2)
# for item in res:
# print(item)
# print(next(res))
# print(next(res))
# print(next(res))
# print(next(res))
# print(res)
#總結yield:
#1. 提供一種自定義迭代器的解決方案
#2. yield vs return:
# 相同點:都能返回值,並且返回值沒有型別與個數限制
# 不同點:yield可以返回值多次值,而return只能返回一次值
# 列表生成式
# res=[]
# for item in range(1,11):
# if item > 5:
# res.append(item)
# print(res)
# res=[item for item in range(1,11) if item > 5]
# print(res)
# names=['alex_sb','egon','kevin_sb','hxx_sb','wxx_sb']
# res=[name for name in names if name.endswith('sb')]
# print(res)
# 字典生成式
# res={i:i**2 for i in range(10) if i > 5}
# print(res)
# l=[('name','egon'),('age',18),('gender','male')]
# dic={k:v for k,v in l if k!='age'}
# print(dic)
# res={i for i in range(10)}
# print(res,type(res))
# 生成器表示式
res = (x for x in range(1, 5))
# print(res)
# print(next(res))
# print(next(res))
# print(next(res))
# print(next(res))
# print(next(res))
with open('aaa.txt',encoding='utf-8') as f:
# g=(len(line) for line in f)
# print(max(g))
res=max(len(line) for line in f)
print(res)