readlines()是否在Python 3中返回列表或迭代器? [英] Does readlines() return a list or an iterator in Python 3?

查看:515
本文介绍了readlines()是否在Python 3中返回列表或迭代器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过潜入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 。我怎么检查?

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.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.

>>>

这篇关于readlines()是否在Python 3中返回列表或迭代器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆