Slicing an array in groups of eight

Vlastimil Brom vlastimil.brom at gmail.com
Thu May 21 23:40:41 CEST 2009


2009/5/21 Graham Arden <trigfa at googlemail.com>:
> 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)
>
...>
> Alternatively is there a simpler way of producing the array above?
>
> Thanks
>
> Graham.
>

Hi,
I'm not sure, if I got the requirements for your code completely, but is:
[x for x in range(512) if not (x // 8) % 2]
what you need?

In any case, if you use python lists you should be able to join them
using their extend() or itertools.chain()

###########################

print [x for x in range(512) if not (x // 8) % 2]

nested_lst = [[1,2],[4,5],[6,7,8,9],[10,11,12]]
flattened_list_1 = list(itertools.chain(*nested_lst))
print flattened_list_1

flattened_list_2 = []
for sublist in nested_lst:
    flattened_list_2.extend(sublist)
print flattened_list_2

hth
  vbr



More information about the Python-list mailing list