对python中的iter()函数与next()函数详解
作者:幸福清风 时间:2022-01-29 19:05:36
list、tuple等都是可迭代对象,我们可以通过iter()函数获取这些可迭代对象的迭代器。然后我们可以对获取到的迭代器不断使⽤next()函数来获取下⼀条数据。iter()函数实际上就是调⽤了可迭代对象的 __iter__ ⽅法。
>>> li = [11, 22, 33, 44, 55]
>>> li_iter = iter(li)
>>> next(li_iter) 11
>>> next(li_iter) 22
>>> next(li_iter) 33
>>> next(li_iter) 44
>>> next(li_iter) 55
>>> next(li_iter)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
>>>
注意:当我们已经迭代完最后⼀个数据之后,再次调⽤next()函数会抛出 StopIteration的异常,来告诉我们所有数据都已迭代完成,不⽤再执⾏ next()函数了。
来源:https://blog.csdn.net/xun527/article/details/76652189
标签:python,iter,next
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SQL Server开发过程中的的常见问题总结
2009-01-06 11:16:00
PHP中获取文件创建日期、修改日期、访问时间的方法
2023-06-11 18:37:15
window.location.hash的应用及浏览器的支持测试
2009-07-07 11:52:00
![](https://img.aspxhome.com/file/UploadPic/20097/7/hashdemo-57s.png)
golang1.16新特性速览(推荐)
2023-07-06 10:34:47
ASP网站程序自动升级实现的方法
2008-10-15 14:28:00
浅析php中array_map和array_walk的使用对比
2023-09-10 22:22:28
一场关于YUI3/jQuery的精彩辩论
2010-11-11 12:50:00
asp源码如何显示数据库字段的结构?
2010-06-08 09:35:00
jquery的$(document).ready()和onload的加载顺序
2023-08-23 18:57:40
PHP实现批量生成App各种尺寸Logo
2023-07-23 03:59:59
浅谈MySQL存储引擎选择 InnoDB还是MyISAM
2009-05-10 20:02:00
对设计文档的一点小看法
2010-03-15 12:30:00
ASP日期和时间函数用法详解
2007-10-13 19:33:00
设计之外随谈
2009-06-16 14:38:00
![](https://img.aspxhome.com/file/UploadPic/20096/16/anddy_xiantan-64s.jpg)
Mysql日期和时间函数大全
2011-03-08 09:52:00
巧用XSL和ASP在线编辑XML文档
2008-10-25 14:54:00
Python编程中NotImplementedError的使用方法
2023-06-08 15:40:40
python运行cmd命令行的3种方法总结
2023-07-15 18:40:49
使用SQL语句,查第10-20条记录
2008-02-19 18:34:00
Go语言实现新春祝福二维码的生成
2023-10-16 06:14:32
![](https://img.aspxhome.com/file/2023/0/95360_0s.png)