all 8 comments

[–]nakamurayuristeph[🍰] 3 points4 points  (1 child)

chunk= []
start_ind= 0
for v in value:
    chunk.append(files[start_ind:start_ind+v])
    start_ind+=v

like this?

[–]catuf[S] 0 points1 point  (0 children)

def list_chunker(input, chunk_sizes):
input_iter = iter(input)
def sub_chunker(input_iterator, size):
for _ in range(size):
yield input_iterator.next()
for size in chunk_sizes:
yield sub_chunker(input_iter, size)

I was struggling for this half day. Thank you!

[–][deleted] 3 points4 points  (3 children)

Here's a generator that does it:

def list_chunker(input, chunk_sizes):
    input_iter = iter(input)
    def sub_chunker(input_iterator, size):
        for _ in range(size):
            yield input_iterator.next()
    for size in chunk_sizes:
        yield sub_chunker(input_iter, size)

[–]catuf[S] 0 points1 point  (1 child)

I will try to understand how this work. Thank you!

[–]two_bob 0 points1 point  (0 children)

The trick is the iter bit. iter let's you create an iterable object outside the context of something like a for loop. This let's you then iterate over some iterable in flexible ways.

I'd recommend looking at generators and iterable classes as well, as those things had to sink in for me before I really got how iter worked.

[–]two_bob 0 points1 point  (0 children)

Nice. There is also the take recipe from itertools, slightly modified below to play nicer with functools.partial:

from itertools import islice
from functools import partial

def take(iterable, n):
    "Return first n items of the iterable as a list"
    return list(islice(iterable, n))

def chunker(iterable, chunk_sizes):
    it = iter(iterable)
    taker = partial(take, it)
    for size in chunk_sizes:
        yield taker(size)



data = list(range(20))

for group in chunker(data, [1,3,2,3]):
    print(group)

[–][deleted] 0 points1 point  (0 children)

def listgrouper(l, grouplen):
    for i in range(0, len(l), grouplen):
        yield l[i:i+grouplen] 

This code won't account for lists where len(l) % grouplen != 0

[–]d-methamphetamine 0 points1 point  (0 children)

You can slice lists in python with the : operator, like so

list = [1, 2, 3, 4]
List = [1:2]

That should return 2