码迷,mamicode.com
首页 > 其他好文 > 详细

如何对迭代器做切片操作?

时间:2020-07-08 23:14:53      阅读:73      评论:0      收藏:0      [点我收藏+]

标签:代码   itertools   down   rto   oge   each   gate   readlines   ever   

需求:
有某个文本文件,我们想读取其中某范围内容如100-300行之间的内容,python中文本文件是可迭代对象,我们是否可以使用类似列表切片的方式得到一个100-300行文件内容的生成器?
f = open(‘/var/log/dmesg‘)
f[100:300] # 可以么?
思路:
1、f = f = open(‘G:\python study\shizhan\english.txt‘) ,lines = f.readlines(),lines[100:300],这样是可以,但是文件太大容易撑爆内存。
2、使用标准库中的itertools.islice,它能返回一个迭代对象切片的生成器
代码:
english.txt:

The place I cherish most in my memory is called The Protection Community which used to be my home for 12 years. It looked neither splendid nor noble, but the cozy and plain one haunted often in my mind.

While I was 11, it was declared to be reconstructed to an instant noodles factory so that we had to move away.

I still remember the vague structure of it. The entrance of the community stood a huge iron gate. On the left of the gate was a buffet, the shopkeeper of which was also in charged as a guard. On the right of the gate was a stall for breakfast, the shopkeeper of which used to my neighbor. As you walked into the gate, there was a long main road straight forwards to a black pot. It witnessed many things of mine as a fresh starter such as the first time to ride a bicycle, the first time to skate, the first time to fly a kite, etc. It was more than I can count. Every night of the weekend we would gather on this road to have fun. It was a habit more than a gather for us. There were three buildings ranked one after one on the left of the road. They were not the same as what we have now. Each layer had seven home residents, and the household were the side by side instead of facing with another. Also the doors were made of screen opened for a whole day instead of thick iron closed for a whole day. During the hot days, we all took out our mats on the corridor, I really miss the life there. On the right of the road you could some

bungallowsand, pool and cropland During the summer, we might swim in the pool as well as playing with frogs, butterflies, dragonflies, mantis and locusts in the cropland. It’s a pity that I have rarely seen some of these species these days. At the end of the road was a garage opened by my classmate’s parents. All the kids in our community were almost in the same school and even some of us were in the same class, we went to school together in a tricycle which is not so common now. That’s also made us more close to each other.

How I wish I could have a camera at that time so that I could record down the precious fragment of my life in the past few years!
from itertools import islice
# import sys,os
# sys.path.append(os.path.dirname(__file__))

f = open(‘shizhan\english.txt‘)

slice_iterator = islice(f,2,10)

#slice(iterable, start, stop[, step]) --> islice object,start可省略,islice(f,100)表示直接从头到100行,islice(f,10,None)表示从10行到结尾

for x in slice_iterator:
    print(x)

如何对迭代器做切片操作?

标签:代码   itertools   down   rto   oge   each   gate   readlines   ever   

原文地址:https://www.cnblogs.com/Richardo-M-Q/p/13269581.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!