久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

內(nèi)置函數(shù) iter() 如何將 Python 列表轉(zhuǎn)換為迭代器

How does the built-in function iter() convert a Python list to an iterator?(內(nèi)置函數(shù) iter() 如何將 Python 列表轉(zhuǎn)換為迭代器?)
本文介紹了內(nèi)置函數(shù) iter() 如何將 Python 列表轉(zhuǎn)換為迭代器?的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我已經(jīng)閱讀了我的材料,其中告訴 Python 迭代器必須同時(shí)具有 __iter____next__ 方法,但迭代器只需要 __iter__.

I have read my materials, which tell that a Python iterator must have both __iter__ and __next__ methods, but an iterable just needs __iter__.

我檢查了一個(gè)列表,發(fā)現(xiàn)它沒(méi)有 __next__ 方法.當(dāng)在其上使用 iter() 時(shí),它將成為一個(gè)迭代器.這是否意味著 iter() 會(huì)在列表中添加一個(gè) __next__ 方法以將其轉(zhuǎn)換為迭代器?

I check a list and find it has no __next__ method. When using iter() on it, it will become an iterator. Does this mean that iter() will add a __next__ method to a list to convert it to an iterator?

如果是,這是怎么發(fā)生的?

If yes, how does this happen?

推薦答案

沒(méi)有.iter 返回一個(gè)迭代器,它不會(huì)將列表轉(zhuǎn)換為迭代器.它根本不修改列表,當(dāng)然,列表沒(méi)有得到 __next__ 方法.

No. iter returns an iterator, it does not convert the list into an iterator. It doesn't modify the list at all, and certainly, the list does not get a __next__ method.

>>> x = [1,2]
>>> it = iter(x)
>>> it
<list_iterator object at 0x101c021d0>
>>> x.__next__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute '__next__'
>>>

列表是iterables,而不是iterators.它們實(shí)現(xiàn)了一個(gè) __iter__ 方法,因此它們是可迭代的:

Lists are iterables, not iterators. They implement a __iter__ method, thus they are iterable:

>>> x.__iter__
<method-wrapper '__iter__' of list object at 0x101bcf248>

但不是 __next__,因此它們不是迭代器:

But not __next__, thus they are not iterators:

>>> next(x)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'list' object is not an iterator

根據(jù)定義,迭代器本身是可迭代的,因?yàn)樗鼈円矊?shí)現(xiàn)了 __iter__.考慮:

Iterators themselves are iterable, by definition, since they implement __iter__ as well. Consider:

>>> x = [1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> it = iter(x)
>>> it
<list_iterator object at 0x101c02358>
>>> it.__iter__
<method-wrapper '__iter__' of list_iterator object at 0x101c02358>

大多數(shù)迭代器應(yīng)該在你對(duì)它們使用iter時(shí)簡(jiǎn)單地返回它們自己:

Most iterators should simply return themselves when you use iter on them:

>>> it2 = iter(it)
>>> it, it2
(<list_iterator object at 0x101c02358>, <list_iterator object at 0x101c02358>)
>>> it is it2
True
>>>

確實(shí),這是迭代器協(xié)議的要求:

"迭代器必須有一個(gè)返回的 __iter__() 方法迭代器對(duì)象本身,因此每個(gè)迭代器也是可迭代的,并且可能是在接受其他迭代的大多數(shù)地方使用."

"Iterators are required to have an __iter__() method that returns the iterator object itself so every iterator is also iterable and may be used in most places where other iterables are accepted."

再次注意,它們是同一個(gè)迭代器:

>>> next(it)
1
>>> next(it2)
2
>>> next(it)
3
>>> next(it)
4
>>> next(it2)
5
>>> list(it)
[6, 7, 8, 9]
>>> next(it2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration

所以一個(gè)iterator實(shí)現(xiàn)了__iter____next__,一個(gè)iterable僅僅意味著它實(shí)現(xiàn)了__iter__.返回 __iter__ 是一個(gè)迭代器,所以它必須實(shí)現(xiàn) __next__.

So an iterator implements __iter__ and __next__, an iterable just means that it implements __iter__. What is returned by __iter__ is an iterator, so that must implement __next__.

這篇關(guān)于內(nèi)置函數(shù) iter() 如何將 Python 列表轉(zhuǎn)換為迭代器?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Troubles while parsing with python very large xml file(使用 python 解析非常大的 xml 文件時(shí)出現(xiàn)問(wèn)題)
Find all nodes by attribute in XML using Python 2(使用 Python 2 在 XML 中按屬性查找所有節(jié)點(diǎn))
Python - How to parse xml response and store a elements value in a variable?(Python - 如何解析 xml 響應(yīng)并將元素值存儲(chǔ)在變量中?)
How to get XML tag value in Python(如何在 Python 中獲取 XML 標(biāo)記值)
How to correctly parse utf-8 xml with ElementTree?(如何使用 ElementTree 正確解析 utf-8 xml?)
Parse XML from URL into python object(將 XML 從 URL 解析為 python 對(duì)象)
主站蜘蛛池模板: 中文字幕视频一区 | 99自拍视频 | 国产a区 | 亚洲最色网站 | 久久久国产一区 | 精品国产鲁一鲁一区二区张丽 | 久久精品国产一区二区电影 | 久久久高清 | 亚洲情综合五月天 | 精品国产乱码久久久久久影片 | 亚洲第一福利视频 | 久久狠狠 | 久久久精品网 | 日本色综合 | 久久成人精品 | 久久久精彩视频 | 国产精品毛片av一区 | 亚洲欧美aⅴ | 欧美日韩电影一区 | 国产精品久久av | 国产97碰免费视频 | 午夜久久久| 亚洲一区二区三区在线 | 欧美日韩精品一区二区三区视频 | 台湾佬伊人 | 91成人免费 | 日韩在线观看精品 | 久久久精品网 | 激情婷婷 | 亚洲视频免费在线观看 | 欧州一区| 亚洲日韩欧美一区二区在线 | 欧美中文| 999久久久免费精品国产 | 91p在线观看 | 激情综合五月天 | 亚洲97| 国产毛片久久久久久久久春天 | 免费久久视频 | 久久久青草婷婷精品综合日韩 | 亚洲精品毛片av |