如何对迭代器做切片操作
来源:互联网 发布:神奇的印度人 知乎 编辑:程序博客网 时间: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.
阅读全文
0 0
- 如何对迭代器做切片操作
- python-9-如何对迭代器做切片操作?
- python如何对list进行切片操作
- 3-5对迭代器做切片操作
- Python高级编程-如何对迭代器进行切片操作?
- python 3-5 如何对迭代器做切片操作itertools.islice(open("test.txt"),0,10),迭代器消耗
- R 语言 数据切片,对每个切片组分别操作
- [Python] 对字符串,列表的切片操作
- 切片操作
- 切片操作
- java 对reids的操作 切片与非切片连接池的应用
- java 对reids的操作 切片与非切片连接池的应用
- FFMPEG-如何对视频按时长切片与压缩
- 对字符串切片
- python对字符串切片
- 对list进行切片
- 对字符串切片
- Python 切片操作
- 安卓开发debug-app卸载不干净,导致安装、升级不成功问题的解决办法
- k&R scanf 函数
- [LeetCode]121. Best Time to Buy and Sell Stock
- 动态稀疏矩阵对应的三元组的输出,以及逆置和赋值算法
- VS 安装助手,弹出“The security key for this program currently stored on your system does not appear to be ”
- 如何对迭代器做切片操作
- 机器视觉缺陷检测-边学边做-相机选取
- 展开一个嵌套的列表
- HDU 1027 Ignatius and the Princess II(逆康托展开)
- 迭代器模式--没错就是Iterator
- hdu2845
- PAT-二叉搜索树
- 杭电acm2524矩形A + B(水题)
- FreeMaker教程(一)-----FreeMaker简介