正确理解python迭代器与生成器
作者:凌逆战 时间:2022-09-14 22:55:57
一、迭代器
迭代器就是iter(可迭代对象函数)返回的对象,说人话.......可迭代对象由一个个迭代器组成
可以用next()函数获取可迭代对象的数据
迭代是访问集合元素的一种方式(因为集合是无序的,所以不能索引),naxt(集合),
迭代器对象从集合的第一个元素开始访问,直到所有元素被访问结束,迭代器只能往前不会往后退
迭代器是一个可以记住遍历的位置的对象,迭代器只能一个一个出,前一秒还没有这个值,等到next()的时候才生成
迭代器有两个基本方法:iter()和next()
iter()生成一个迭代器
next()从迭代器中获取写一条记录,如果无法获取写一条记录,则触发StopIteration异常
有序序列:字符串、列表、元组对象都可用于创建迭代器
L = [1,2,3,4]
it = iter(L) #创建迭代器对象
print(next(it)) #输出迭代器的写一个对象
print(next(it))
迭代器对象使用for语句进行遍历
li = [5,6,7,8]
it = iter(li)
for x in it:
print(x,end = ' ')
迭代器对象使用while语句进行遍历
lis = [7,8,9,0]
it = iter(lis)
while True:
try:
print(next(it))
except StopIteration:
break
二、生成器
在python中,使用了yield的函数被称为生成器(generator)此函数被调用时返回一个生成器对象
生成器是一个返回迭代器的函数,生成器 生成 迭代器对象,只能用来迭代操作
生成器能让函数停下下,想进想出,很随便,前一秒数据根本不存在,这一秒推算出来的
在调用生成器运行的过程中,每次遇到yidld时函数会停下来,返回yield的值,相当于print返回print的值一样
并在下一次执行next()方法或者写一次循环时从当前位置继续运行(继续打印下一个值)
生成器用于函数中,会把函数当做生成器来使用,函数出一个值,主函数调取一个值
生成器调用return会触发一个StopIteration异常
普通方法生成菲波那切数列
def fun(n):
a,b,c = 0,1,0
while c<n:
print(b) # 打印菲波那切数列
a,b = b,a+b
c +=1
fun(10)
用生成器的方法生成菲波那切数列
def fun(n):
a,b,c = 0,1,0
while c<n:
yield b # 生成器
a,b = b,a+b
c +=1
# print(fun(10))# <generator object fun at 0x000001ED43A48A40>
t = fun(10) # t是一个迭代器,由生成器返回生成
print(next(t)) # 1
print(next(t)) # 1
print("中间可以插入代码") # 中间可以插入代码
print(next(t)) # 2
print(next(t)) # 3
for i in t:
print(i)
# 8
# 13
# 21
# 34
# 55
print(fun(10))
# <generator object fun at 0x000001ED43A48A40>
# 这是一条内存,你要用函数来访问里面的值,next(fun(10))
生成器表达式:
语法:(表达式 for 变量in 可迭代对象 [if 真值表达式]) []里的内容可以省略
作用:用推导式的形式生成一个新的生成器,要取值的时候,要iter变成迭代器,用next取值
优点:不占用内存空间
迭代工具函数:生成一个个个的可迭代对象
gen = (x**2 for x in range(1, 4))
it = iter(gen) # 转成生成器
next(it)# 1
next(it)# 4
next(it)# 9
next(it)# StopIteration
三、生成器函数
3.1、zip(可迭代对象1,可迭代对象2......)
返回一个zip对象,此对象用于生成元组,元组的个数由最小的可迭代对象决定
numbers = [10086,10000,10010,95586]
names = ['中国移动','中国联通','中国电信']
for t in zip(numbers,names):
print(t)
#(10086, '中国移动')
#(10000, '中国联通')
#(10010, '中国电信')
自定义zip函数
def myzip(iter1,iter2):
it1 = iter(iter1) # 拿出一个迭代器
it2 = iter(iter2)
while True:
a = next(it1)
b = next(it2)
yield (a,b)
numbers = [10086,10000,10010,95586]
names = ['中国移动','中国联通','中国电信']
for t in myzip(numbers,names):
print(t)
# (10086, '中国移动')
# (10000, '中国联通')
# (10010, '中国电信')
3.2、enumerate(iterable[,start])
生成带索引的枚举对象,返回迭代类型为索引-值对(index,value)对, 默认索引从零开始,也可以使用start绑定
names = ['中国移动', '中国电信', '中国联通']
for x in enumerate(names): #生成迭代器
print(x)
def myenumerate(iterable):
it = iter(iterable)
i = 0
while True:
a = next(it)
yield(i,a)
i += 1
#(0, '中国移动')
#(1, '中国电信')
#(2, '中国联通')
来源:https://www.cnblogs.com/LXP-Never/p/9338189.html
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
简单解析PHP程序的运行流程
![](https://img.aspxhome.com/file/2023/1/55481_0s.gif)
Python办公自动化之Excel(中)
这些CSS Selector,你都熟悉吗?
详解Python3.1版本带来的核心变化
numpy中np.c_和np.r_的用法解析
asp 随机字符串函数
利用Python的Django框架生成PDF文件的教程
Python 数据可视化超详细讲解折线图的实现
![](https://img.aspxhome.com/file/2023/9/90879_0s.png)
Blender Python编程实现程序化建模生成超形示例详解
![](https://img.aspxhome.com/file/2023/8/128518_0s.png)
Pycharm Git 设置方法
![](https://img.aspxhome.com/file/2023/7/115717_0s.png)
python文件操作之批量修改文件后缀名的方法
python 装饰器功能以及函数参数使用介绍
![](https://img.aspxhome.com/file/2023/4/67794_0s.png)
po+selenium+unittest自动化测试项目实战
![](https://img.aspxhome.com/file/2023/1/80451_0s.png)
jupyter lab文件导出/下载方式
![](https://img.aspxhome.com/file/2023/6/128326_0s.jpg)
巧妙规划使用Oracle数据空间
什么样的图标更具有可用性
![](https://img.aspxhome.com/file/UploadPic/200710/16/20071016174919899s.jpg)
Python 列表与链表的区别详解
![](https://img.aspxhome.com/file/2023/5/70905_0s.png)
python2.7读取文件夹下所有文件名称及内容的方法
基于python实现双向链表
![](https://img.aspxhome.com/file/2023/9/99219_0s.jpg)
python实现按键精灵找色点击功能教程,使用pywin32和Pillow库
![](https://img.aspxhome.com/file/2023/5/63915_0s.jpg)