1

I have the following example code:

l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

def batch_gen(data, batch_size):
    for i in range(0, len(data), batch_size):
        yield data[i:i+batch_size]
batch_size = randint(1,4)
n = 0
print("batch_size is {}".format(batch_size))
for i in batch_gen(l, batch_size):
    for x in i:
        print(x)
    print("loop {}".format(n))
    n += 1
    batch_size = randint(1,4)

Which gives me the output of:

batch_size is 3
1
2
3
loop 0
4
5
6
loop 1
7
8
9
loop 2
10
loop 3

This is the output I'm looking for however batch_size is always set from batch_size = randint(1,4) outside of the for loop.

I'm looking at having a random batch_size for each iteration of the loop, and each next loop picking up where the last loop left off until completion of a random length list.

Any help would be greatly appreciated!

Example code taken from Iterate over a python sequence in multiples of n?

simonebone
  • 11
  • 1

0 Answers0