Slicing an array in groups of eight

G

Graham Arden

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/
 
V

Vlastimil Brom

2009/5/21 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)
....>
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
 
E

Emile van Sebille

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
 
R

Robert Kern

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top