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

Posted richardo-m-q

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何对迭代器做切片操作?相关的知识,希望对你有一定的参考价值。

需求:
有某个文本文件,我们想读取其中某范围内容如100-300行之间的内容,python中文本文件是可迭代对象,我们是否可以使用类似列表切片的方式得到一个100-300行文件内容的生成器?
f = open(‘/var/log/dmesg‘)
f[100:300] # 可以么?
思路:
1、f = f = open(‘G:python studyshizhanenglish.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(‘shizhanenglish.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)








以上是关于如何对迭代器做切片操作?的主要内容,如果未能解决你的问题,请参考以下文章

Python中迭代器&生成器的“奇技淫巧“

使用迭代器做自定义筛选

你知道的Go切片扩容机制可能是错的

带有迭代的 Pandas DataFrame 切片

如何迭代切片?

Python3 高级特性