Iteration over list slices

后端 未结 9 624
名媛妹妹
名媛妹妹 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:52

    Do you mean something like:

    def callonslices(size, fatherList, foo):
      for i in xrange(0, len(fatherList), size):
        foo(fatherList[i:i+size])
    

    If this is roughly the functionality you want you might, if you desire, dress it up a bit in a generator:

    def sliceup(size, fatherList):
      for i in xrange(0, len(fatherList), size):
        yield fatherList[i:i+size]
    

    and then:

    def callonslices(size, fatherList, foo):
      for sli in sliceup(size, fatherList):
        foo(sli)
    

提交回复
热议问题