如何对迭代器做切片操作

来源:互联网 发布:神奇的印度人 知乎 编辑:程序博客网 时间:2024/05/29 16:29
实际案例

有某个文本文件,我们想读取其中某范围的内容如100~300行之间的内容,Python中文本文件是可迭代对象,我们是否可以使用类似列表切片的方式得到一个100~300行文件内容的生成器?

首先我们打开一个文本文件,利用readlines()读取文本文件的每一行至列表中,我们再对列表进行切片操作,其代码如下:

# -*- coding: utf-8 -*-f = open('test.txt')lines = f.readlines()[2:5]print lines

其输出结果为:

['The State Council issued a guideline on Thursday in developing artificial intelligence (AI), vowing to catch up world advancing levels in artificial intelligence technology and application by the year 2020.\n', "The guideline vows to make artificial intelligence a key economic driving force for China by 2020, while its appliance can help greatly with improving people's livelihood as well as China's innovation capacity. It was also made clear in the guideline that China vows to become an innovation center of artificial intelligence by the year 2030.\n", "Advancing the AI will bring China with new development opportunities, the guideline says, especially when China now faces a set of challenges of an aging population and development restricted by environmental resources. The AI industries are expected to improve public services in education, caring for the elderly as well as urban infrastructure, contributing in improving people's lives and social governance.\n"]

其中test.txt中的文本内容来自扇贝新闻。由于其内容较少,故只读取2~4行的之间的内容。

虽然,我们使用readlines()可以解决该问题,但这种方法有个明显的缺陷,就是我们使用readlines()后,它会把我们的数据存放在内存中,如果我们的文件有几个G的话,那我们的内存就不够存储了。因此,我们不推荐使用该方法处理该问题。

这里,我们推荐使用标准库中的itertools.islice,它能返回一个迭代对象切片的生成器,具体代码如下:

# -*- coding: utf-8 -*-from itertools import islicef = open('test.txt')for line in islice(f, 2, 5):    print line

其输出结果为:

The State Council issued a guideline on Thursday in developing artificial intelligence (AI), vowing to catch up world advancing levels in artificial intelligence technology and application by the year 2020.The guideline vows to make artificial intelligence a key economic driving force for China by 2020, while its appliance can help greatly with improving people's livelihood as well as China's innovation capacity. It was also made clear in the guideline that China vows to become an innovation center of artificial intelligence by the year 2030.Advancing the AI will bring China with new development opportunities, the guideline says, especially when China now faces a set of challenges of an aging population and development restricted by environmental resources. The AI industries are expected to improve public services in education, caring for the elderly as well as urban infrastructure, contributing in improving people's lives and social governance.
原创粉丝点击