readlines() 在 Python 3 中返回列表还是迭代器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3541203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:38:53  来源:igfitidea点击:

Does readlines() return a list or an iterator in Python 3?

pythoniteratorpython-3.xreadlines

提问by snakile

I've read in "Dive into Python 3" that "The readlines() method now returns an iterator, so it is just as efficient as xreadlines() was in Python 2". See here: http://diveintopython3.org/porting-code-to-python-3-with-2to3.html. I'm not sure that it's true because they don't mention it here: http://docs.python.org/release/3.0.1/whatsnew/3.0.html. How can I check that?

我在“深入 Python 3”中读到“readlines() 方法现在返回一个迭代器,因此它与 Python 2 中的 xreadlines() 一样有效”。请参阅此处:http: //diveintopython3.org/porting-code-to-python-3-with-2to3.html。我不确定这是真的,因为他们没有在这里提到它:http: //docs.python.org/release/3.0.1/whatsnew/3.0.html。我该如何检查?

采纳答案by John Machin

Like this:

像这样:

Python 3.1.2 (r312:79149, Mar 21 2010, 00:41:52) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> f = open('/junk/so/foo.txt')
>>> type(f.readlines())
<class 'list'>
>>> help(f.readlines)
Help on built-in function readlines:

readlines(...)
    Return a list of lines from the stream.

    hint can be specified to control the number of lines read: no more
    lines will be read if the total size (in bytes/characters) of all
    lines so far exceeds hint.

>>>

回答by Scott Griffiths

The readlines method doesn't return an iterator in Python 3, it returns a list

readlines 方法在 Python 3 中不返回迭代器,它返回一个列表

Help on built-in function readlines:

readlines(...)
    Return a list of lines from the stream.

To check, just call it from an interactive session - it will return a list, rather than an iterator:

要检查,只需从交互式会话中调用它 - 它会返回一个列表,而不是一个迭代器:

>>> type(f.readlines())
<class 'list'>

Dive into Python appears to be wrong in this case.

在这种情况下,深入 Python 似乎是错误的。



xreadlineshas been deprecated since Python 2.3when file objects became their own iterators. The way to get the same efficiency as xreadlinesis instead of using

xreadlines当文件对象成为它们自己的迭代器时,自 Python 2.3 起已被弃用。获得与原样相同效率的方法,xreadlines而不是使用

 for line in f.xreadlines():

you should use simply

你应该简单地使用

 for line in f:

This gets you the iterator that you want, and helps to explain why readlinesdidn't need to change its behaviour in Python 3 - it can still return a full list, with the line in fidiom giving the iterative approach, and the long-deprecated xreadlineshas been removed completely.

这为您提供了您想要的迭代器,并有助于解释为什么readlines不需要在 Python 3 中更改其行为 - 它仍然可以返回一个完整的列表,line in f惯用语给出了迭代方法,并且长期不推荐使用的xreadlines已被删除完全地。

回答by Hyman O'Connor

Others have said as much already, but just to drive the point home, ordinary file objects are their own iterators. So having readlines()return an iterator would be silly, because it would just return the file you called it on. You can use a forloop to iterate over a file, like Scott said, and you can also pass them straight to itertools functions:

其他人已经说了这么多,但只是为了说明问题,普通文件对象是它们自己的迭代器。所以readlines()返回一个迭代器会很愚蠢,因为它只会返回你调用它的文件。您可以使用for循环来遍历文件,就像 Scott 所说的那样,您也可以将它们直接传递给 itertools 函数:

from itertools import islice
f = open('myfile.txt')
oddlines = islice(f, 0, None, 2)
firstfiveodd = islice(oddlines, 5)
for line in firstfiveodd:
  print(line)