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

對(duì) python 列表感到困惑:它們是還是不是迭代器?

Confused with python lists: are they or are they not iterators?(對(duì) python 列表感到困惑:它們是還是不是迭代器?)
本文介紹了對(duì) python 列表感到困惑:它們是還是不是迭代器?的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我正在研究 Alex Marteli 的 Python 簡(jiǎn)述 并且本書(shū)建議任何具有 next() 方法的對(duì)象都是(或至少可以用作)迭代器.它還表明,大多數(shù)迭代器是通過(guò)對(duì)名為 iter 的方法的隱式或顯式調(diào)用來(lái)構(gòu)建的.

I am studying Alex Marteli's Python in a Nutshell and the book suggests that any object that has a next() method is (or at least can be used as) an iterator. It also suggests that most iterators are built by implicit or explicit calls to a method called iter.

讀完這本書(shū)后,我有一種嘗試的沖動(dòng).我啟動(dòng)了一個(gè) python 2.7.3 解釋器并這樣做了:

After reading this in the book, I felt the urge to try it. I fired up a python 2.7.3 interpreter and did this:

>>> x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> for number in range(0, 10):
...     print x.next()

然而結(jié)果是這樣的:

Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
AttributeError: 'list' object has no attribute 'next'

在困惑中,我試圖通過(guò) dir(x) 研究 x 對(duì)象的結(jié)構(gòu),我注意到它有一個(gè) __iter__ 函數(shù)對(duì)象.所以我發(fā)現(xiàn)它可以用作迭代器,只要它支持那種類(lèi)型的接口.

In confusion, I tried to study the structure of the x object via dir(x) and I noticed that it had a __iter__ function object. So I figured out that it can be used as an iterator, so long as it supports that type of interface.

所以當(dāng)我再次嘗試時(shí),這次略有不同,嘗試這樣做:

So when I tried again, this time slightly differently, attempting to do this:

>>> _temp_iter = next(x)

我收到了這個(gè)錯(cuò)誤:

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

但是列表怎么可能不是迭代器,因?yàn)樗坪踔С执私涌冢⑶以谝韵律舷挛闹锌隙梢杂米饕粋€(gè):

But how can a list NOT be an iterator, since it appears to support this interface, and can be certainly used as one in the following context:

>>> for number in x:
...     print x

有人可以幫我澄清一下嗎?

推薦答案

它們是可迭代的,但它們不是迭代器.它們可以被傳遞給 iter() 以隱式地(例如通過(guò) for)或顯式地為它們獲取迭代器,但它們本身并不是迭代器.

They are iterable, but they are not iterators. They can be passed to iter() to get an iterator for them either implicitly (e.g. via for) or explicitly, but they are not iterators in and of themselves.

這篇關(guān)于對(duì) python 列表感到困惑:它們是還是不是迭代器?的文章就介紹到這了,希望我們推薦的答案對(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)文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區(qū)域周?chē)L制一個(gè)矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測(cè)和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個(gè)矩形邊界框中應(yīng)用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據(jù)文本方向檢測(cè)圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測(cè)圖像中矩形的中心和角度)
主站蜘蛛池模板: 国产精品黄视频 | 久一久 | 国产一级视频在线观看 | 精品国产不卡一区二区三区 | 超碰在线久| 天天操综合网站 | 老司机久久 | 欧美日韩成人在线 | 黄视频免费观看 | 亚州av | 国产精品看片 | 91.xxx.高清在线 | 亚洲a网| 日韩在线观看中文字幕 | 国产精品毛片 | 在线观看三级av | 91伊人网| 就操在线 | 在线观看视频h | 午夜视频精品 | 四虎成人免费电影 | 国产精品一区二区三区在线 | 中文字幕国产视频 | 欧美一级久久 | 国产成人精品一区二区三区 | 在线观看免费av网 | 久久精品国产清自在天天线 | 欧美一级片久久 | 国产伦一区二区三区久久 | 欧美一级三级 | 欧美高清视频一区 | 日本精品一区二区三区在线观看视频 | 国产综合精品一区二区三区 | 精品国产视频 | 欧洲精品在线观看 | 国产精品免费一区二区 | 日韩a级片| 日韩黄 | 久久国产精品一区二区三区 | 久热国产在线 | 国产免费一区二区三区 |