Python记录12:迭代器+生成器+生成式

'''
1. 什么是迭代器
什么是迭代:迭代就是一个重复的过程,但是每一次重复都是基于上一次的结果而进行的
单纯的重复不是迭代:
while True:
print(1)

迭代的过程
l=['a','b','c']
i=0
while i < len(l):
print(l[i])
i+=1

迭代器:迭代取值的工具

2. 为何要用迭代器
迭代器的优点:
1. 提供了一种可以不依赖索引的迭代取值方式

3. 如何用迭代器
'''
# 可迭代的对象:但凡内置有__iter__方法的对象就是可迭代的对象,例如:strlist upledictset文件对象
# ''.__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:周末三期day061 叠加多个装饰器.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)


原文地址:https://www.cnblogs.com/1832921tongjieducn/p/10070597.html