zoukankan      html  css  js  c++  java
  • 如何对迭代器做切片操作?

    需求:
    有某个文本文件,我们想读取其中某范围内容如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)
    ===================================================
    def my_slice(iterable,start,end,step=1):
        tmp = 0
        for i,x in enumerate(iterable):
            if i >= end:
                break
            
            if i >= start:
                if tmp == 0:
                    tmp = step
                    yield x 
                tmp -= 1  # 当步进值减为0,就返回下一个值
    print(list(my_slice(range(100,150),10,20,3)))
    from itertools import islice
    print(list(islice(range(100,150),10,20,3)))
    
    =========================================================
    >>> f = open('/var/log/dpkg.log.1')
    
    >>> f[100:300]
    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    <ipython-input-21-3cc60f8b989f> in <module>
    ----> 1 f[100:300]
    
    TypeError: '_io.TextIOWrapper' object is not subscriptable
    
    >>> l = list(range(10))
    
    >>> l
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    
    >>> l[2:8]
    [2, 3, 4, 5, 6, 7]
    
    >>> l[3]
    3
    
    >>> l.__getitem__(3)
    3
    
    >>> l.__getitem__(slice(2,8))
    [2, 3, 4, 5, 6, 7]
    
    >>> l[2:8:2}
      File "<ipython-input-28-81cdf22e6b3f>", line 1
        l[2:8:2}
               ^
    SyntaxError: invalid syntax
    
    
    >>> l[2:8:2]
    [2, 4, 6]
    
    >>> l.__getitem__(slice(2,8,2))
    [2, 4, 6]
    
    >>> lines = f.readlines()
    
    >>> isinstance(lines,list)
    True
    
    >>> lines[100:300]
    >>> f = open('/var/log/dpkg.log.1')
    
    >>> from itertools import islice
    
    >>> islice?
    Init signature: islice(self, /, *args, **kwargs)
    Docstring:     
    islice(iterable, stop) --> islice object
    islice(iterable, start, stop[, step]) --> islice object
    
    Return an iterator whose next() method returns selected values from an
    iterable.  If start is specified, will skip all preceding elements;
    otherwise, start defaults to zero.  Step defaults to one.  If
    specified as another value, step determines how many values are 
    skipped between successive calls.  Works like a slice() on a list
    but returns an iterator.
    Type:           type
    Subclasses:     
    
    >>> for line in islice(f,100-1,300):
    ...     print(line)
    >>> range(100,150)
      File "<ipython-input-38-495b03cf8482>", line 1
        range(100,150)
                ^
    SyntaxError: invalid character in identifier
    
    
    >>> range(100,150)
    range(100, 150)
    
    >>> len(range(100, 150))
    50
    
    
  • 相关阅读:
    Linux中的MyEclipse配置Hadoop
    C#学习笔记(三)
    关于读博,关于成为一个专家
    C#查找子串在原串中出现次数
    C#学习笔记(二)
    Matlab中sortrows函数解析
    C#学习笔记(一)
    甘特图与网络图
    ubuntu开启SSH服务
    分词错误重点分析这几项
  • 原文地址:https://www.cnblogs.com/Richardo-M-Q/p/13269581.html
Copyright © 2011-2022 走看看