python中的next()以及iter()函数
更新于2018-02-24 11:51:08
我们首先要知道什么是可迭代的对象(可以用for循环的对象)Iterable:
一类:list,tuple,dict,set,str
二类:generator,包含生成器和带yield的generatoe function
而生成器不但可以作用于for,还可以被next()函数不断调用并返回下一个值,可以被next()函数不断返回下一个值的对象称为迭代器:Iterator
生成器都是Iterator对象,但list,dict,str是Iterable,但不是Iterator,要把list,dict,str等Iterable转换为Iterator可以使用iter()函数
next()用法:
next(iterator[, default])
- iterator -- 可迭代对象
- default -- 可选,用于设置在没有下一个元素时返回该默认值,如果不设置,又没有下一个元素则会触发 StopIteration 异常。
-
>>> list_ = [
1,
2,
3,
4,
5]
-
>>> it = iter(list_)
-
>>> next(it,
'-1')
-
1
-
>>> next(it,
'-1')
-
2
-
>>> next(it,
'-1')
-
3
-
>>> next(it,
'-1')
-
4
-
>>> next(it,
'-1')
-
5
-
>>> next(it,
'-1')
-
'-1'
参考:
https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/00143178254193589df9c612d2449618ea460e7a672a366000
<li class="tool-item tool-active is-like "><a href="javascript:;"><svg class="icon" aria-hidden="true"> <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#csdnc-thumbsup"></use> </svg><span class="name">点赞</span> <span class="count">2</span> </a></li> <li class="tool-item tool-active is-collection "><a href="javascript:;" data-report-click="{"mod":"popu_824"}"><svg class="icon" aria-hidden="true"> <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#icon-csdnc-Collection-G"></use> </svg><span class="name">收藏</span></a></li> <li class="tool-item tool-active is-share"><a href="javascript:;"><svg class="icon" aria-hidden="true"> <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#icon-csdnc-fenxiang"></use> </svg>分享</a></li> <!--打赏开始--> <!--打赏结束--> <li class="tool-item tool-more"> <a> <svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg> </a> <ul class="more-box"> <li class="item"><a class="article-report">文章举报</a></li> </ul> </li> </ul> </div> </div> <div class="person-messagebox"> <div class="left-message"><a href="https://blog.csdn.net/li1615882553"> <img src="https://profile.csdnimg.cn/2/8/A/3_li1615882553" class="avatar_pic" username="li1615882553"> <img src="https://g.csdnimg.cn/static/user-reg-year/1x/3.png" class="user-years"> </a></div> <div class="middle-message"> <div class="title"><span class="tit"><a href="https://blog.csdn.net/li1615882553" data-report-click="{"mod":"popu_379"}" target="_blank">阿_波_</a></span> </div> <div class="text"><span>发布了229 篇原创文章</span> · <span>获赞 88</span> · <span>访问量 13万+</span></div> </div> <div class="right-message"> <a href="https://im.csdn.net/im/main.html?userName=li1615882553" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-letter">私信 </a> <a class="btn btn-sm bt-button personal-watch" data-report-click="{"mod":"popu_379"}">关注</a> </div> </div> </div> </article>