Slicing an array in groups of eight

Robert Kern robert.kern at gmail.com
Thu May 21 18:14:23 EDT 2009


On 2009-05-21 15:51, Graham Arden wrote:
> 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?

b = a.reshape((-1, 8))

You will want to ask numpy questions on the numpy mailing list:

   http://www.scipy.org/Mailing_Lists

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco




More information about the Python-list mailing list