Slicing an array in groups of eight

Graham Arden trigfa at googlemail.com
Thu May 21 16:51:56 EDT 2009


A python novice writes.....

Hello,

I'm trying to extract certain frames from a stack of images as part of
a project.  In order to do this I need to produce an array which
consists of a group of eight, then misses the next 8, then selects the
next eight etc.

i.e (0, 1, 2, 3, 4, 5, 6, 7, 16, 17,18, 19,20,21, 22, 23, 32,33,....
etc)

The following code will produce a series of arrays:

a = arange (0,512)
b = [a[i:i + 8] for i in range (0, len(a), 16)]

[array([0, 1, 2, 3, 4, 5, 6, 7]),
 array([16, 17, 18, 19, 20, 21, 22, 23]),
 array([32, 33, 34, 35, 36, 37, 38, 39]),
 array([48, 49, 50, 51, 52, 53, 54, 55]),
 array([64, 65, 66, 67, 68, 69, 70, 71]),
 array([80, 81, 82, 83, 84, 85, 86, 87]),
etc...


unfortunately I can't work out a way of joining then into a single
array.

Alternatively is there a simpler way of producing the array above?

Thanks

Graham.
http://surelythatcantberight.blogspot.com/



More information about the Python-list mailing list