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

发布于 2024-09-15 02:37:53 字数 552 浏览 8 评论 0原文

我在“Dive into Python 3”中读到:

readlines() 方法现在返回一个迭代器,因此它与 Python 2 中的 xreadlines() 一样高效”。

请参阅: 附录 A:使用 2to3 将代码移植到 Python 3 :A.26 xreadlines() I/O方法

我不确定这是真的,因为他们没有在这里提及: http://docs.python.org/release/3.0.1/whatsnew/3.0.html。我该如何检查?

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: Appendix A: Porting Code to Python 3 with 2to3: A.26 xreadlines() I/O method.

I'm not sure that'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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

芯好空 2024-09-22 02:37:53

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

Help on built-in function readlines:

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

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

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

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


xreadlines自 Python 2.3 起已弃用对象成为它们自己的迭代器。获得与 xreadlines 相同效率的方法是使用

 for line in f.xreadlines():

你应该简单地使用

 for line in f:

这会让你得到你想要的迭代器,并有助于解释为什么readlines不需要改变它在Python 3中的行为 - 它仍然可以返回一个完整的列表,其中 line in f 习惯用法给出了迭代方法,并且长期弃用的 xreadlines 已被完全删除。

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

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.


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

 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 readlines didn't need to change its behaviour in Python 3 - it can still return a full list, with the line in f idiom giving the iterative approach, and the long-deprecated xreadlines has been removed completely.

垂暮老矣 2024-09-22 02:37:53

像这样:

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.

>>>

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.

>>>
要走干脆点 2024-09-22 02:37:53

其他人已经说过了,但为了说明这一点,普通文件对象是它们自己的迭代器。因此让 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)

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 for loop to iterate over a file, like Scott said, and you can also pass them straight to itertools functions:

from itertools import islice
f = open('myfile.txt')
oddlines = islice(f, 0, None, 2)
firstfiveodd = islice(oddlines, 5)
for line in firstfiveodd:
  print(line)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文