Friday, February 8, 2013

Python spit array into equal parts

My need was to take some C code that read in n-byte chunks from a file, and instead take those chunks from an array instead. The problem was helped by the fact that the size of the source array was always fixed to the same size; it was a simple matter of setting up a loop of array size/chunk size and then setting a chunk-sized array to each chunk repeatedly and processing it. Here is the final code:

for row in range (0,[size/chunk]):
a = data[row*[chunk]:(row*[chunk])+[chunk]]

The little bit at the end is important; to subset arrays, the final element in [n:m] needs to be one larger than you would use in C, which would be [chunk-1].

Here's a really thorough discussion, using an unfamiliar keyword "yield" for several suggestions:

http://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks-in-python/312644