Iterate over a python sequence in multiples of n?

后端 未结 17 723
北荒
北荒 2020-12-01 17:45

How do I process the elements of a sequence in batches, idiomatically?

For example, with the sequence \"abcdef\" and a batch size of 2, I would like to do something

相关标签:
17条回答
  • 2020-12-01 18:42

    From the docs of more_itertools: more_itertools.chunked()

    more_itertools.chunked(iterable, n)
    

    Break an iterable into lists of a given length:

    >>> list(chunked([1, 2, 3, 4, 5, 6, 7], 3))
    [[1, 2, 3], [4, 5, 6], [7]]
    

    If the length of iterable is not evenly divisible by n, the last returned list will be shorter.

    0 讨论(0)
  • 2020-12-01 18:43

    Adapted from this answer for Python 3:

    def groupsgen(seq, size):
        it = iter(seq)
        iterating = True
        while iterating:
            values = ()
            try:
                for n in range(size):
                    values += (next(it),)
            except StopIteration:
                iterating = False
                if not len(values):
                    return None
            yield values
    

    It will safely terminate and won't discard values if their number is not divisible by size.

    0 讨论(0)
  • 2020-12-01 18:44

    One solution, although I challenge someone to do better ;-)

    a = 'abcdef'
    b = [[a[i-1], a[i]] for i in range(1, len(a), 2)]
    
    for x, y in b:
      print "%s%s\n" % (x, y)
    
    0 讨论(0)
  • 2020-12-01 18:46

    Don't forget about the zip() function:

    a = 'abcdef'
    for x,y in zip(a[::2], a[1::2]):
      print '%s%s' % (x,y)
    
    0 讨论(0)
  • 2020-12-01 18:46

    How about itertools?

    from itertools import islice, groupby
    
    def chunks_islice(seq, size):
        while True:
            aux = list(islice(seq, 0, size))
            if not aux: break
            yield "".join(aux)
    
    def chunks_groupby(seq, size):
        for k, chunk in groupby(enumerate(seq), lambda x: x[0] / size):
            yield "".join([i[1] for i in chunk])
    
    0 讨论(0)
提交回复
热议问题