Slicing an array in groups of eight

Emile van Sebille emile at fenx.com
Thu May 21 18:02:18 EDT 2009


On 5/21/2009 1:51 PM Graham Arden said...
> 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)]

How about...

 >>> a = range(0,512)
 >>> b = []
 >>> [b.extend(a[i:i + 8]) for i in range (0, len(a), 16)]
 >>> b
[0, 1, 2, 3, 4, 5, 6, 7, 16, 17, 18, 19, 20, 21, 22, 23, 32,
...
497, 498, 499, 500, 501, 502, 503]
 >>>

Emile




More information about the Python-list mailing list