Iteration over list slices

后端 未结 9 544
名媛妹妹
名媛妹妹 2020-11-30 00:59

I want an algorithm to iterate over list slices. Slices size is set outside the function and can differ.

In my mind it is something like:

for list_of_x         


        
相关标签:
9条回答
  • 2020-11-30 01:57

    If you want to be able to consume any iterable you can use these functions:

    from itertools import chain, islice
    
    def ichunked(seq, chunksize):
        """Yields items from an iterator in iterable chunks."""
        it = iter(seq)
        while True:
            yield chain([it.next()], islice(it, chunksize-1))
    
    def chunked(seq, chunksize):
        """Yields items from an iterator in list chunks."""
        for chunk in ichunked(seq, chunksize):
            yield list(chunk)
    
    0 讨论(0)
  • 2020-11-30 02:01

    Answer to the last part of the question:

    question update: How to modify the function you have provided to store the extra items and use them when the next fatherList is fed to the function?

    If you need to store state then you can use an object for that.

    class Chunker(object):
        """Split `iterable` on evenly sized chunks.
    
        Leftovers are remembered and yielded at the next call.
        """
        def __init__(self, chunksize):
            assert chunksize > 0
            self.chunksize = chunksize        
            self.chunk = []
    
        def __call__(self, iterable):
            """Yield items from `iterable` `self.chunksize` at the time."""
            assert len(self.chunk) < self.chunksize
            for item in iterable:
                self.chunk.append(item)
                if len(self.chunk) == self.chunksize:
                    # yield collected full chunk
                    yield self.chunk
                    self.chunk = [] 
    

    Example:

    chunker = Chunker(3)
    for s in "abcd", "efgh":
        for chunk in chunker(s):
            print ''.join(chunk)
    
    if chunker.chunk: # is there anything left?
        print ''.join(chunker.chunk)
    

    Output:

    abc
    def
    gh
    
    0 讨论(0)
  • 2020-11-30 02:02

    I am not sure, but it seems you want to do what is called a moving average. numpy provides facilities for this (the convolve function).

    >>> x = numpy.array(range(20))
    >>> x
        array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
           17, 18, 19])    
    >>> n = 2 # moving average window
    >>> numpy.convolve(numpy.ones(n)/n, x)[n-1:-n+1]
    array([  0.5,   1.5,   2.5,   3.5,   4.5,   5.5,   6.5,   7.5,   8.5,
             9.5,  10.5,  11.5,  12.5,  13.5,  14.5,  15.5,  16.5,  17.5,  18.5])
    

    The nice thing is that it accomodates different weighting schemes nicely (just change numpy.ones(n) / n to something else).

    You can find a complete material here: http://www.scipy.org/Cookbook/SignalSmooth

    0 讨论(0)
提交回复
热议问题