extract multiple ranges from a list

P

pi.arctan

Hi guys,

One of my many project involves working with YUV-files, where I need
to reduce
the vertical resolution with a factor of two, i.e. remove every other
scan line.
Today I'm using two for-loops in the fashion shown below

y = []
for i in range(0, width*height, width*2):
for j in range(0,width):
y.append(Y[i+j])

This approach doesn't feel very pythonic but I can't come up with a
better idea to do it.
I've tried list comprehension and map together with lambda but I can't
get a flattened list
of every other scan-line...

CIF = 352x288 items for luminance and the aim is to have the list
below:
y = [0:352 704:1056 ... ]

//Fredrik
 
P

Paul Hankin

Hi guys,

One of my many project involves working with YUV-files, where I need
to reduce
the vertical resolution with a factor of two, i.e. remove every other
scan line.
Today I'm using two for-loops in the fashion shown below

y = []
for i in range(0, width*height, width*2):
    for j in range(0,width):
        y.append(Y[i+j])

There's nothing really wrong with your code. Maybe it's a little nicer
written like this:

y = []
for i in range(0, height, 2):
y.extend(Y[i * width + j] for j in range(width))
 
P

Peter Otten

One of my many project involves working with YUV-files, where I need
to reduce
the vertical resolution with a factor of two, i.e. remove every other
scan line.
Today I'm using two for-loops in the fashion shown below

y = []
for i in range(0, width*height, width*2):
for j in range(0,width):
y.append(Y[i+j])

This approach doesn't feel very pythonic but I can't come up with a
better idea to do it.
I've tried list comprehension and map together with lambda but I can't
get a flattened list
of every other scan-line...

CIF = 352x288 items for luminance and the aim is to have the list
below:
y = [0:352 704:1056 ... ]
width = 3; height = 5
Y = range(width*height)
y = []
for i in range(0, width*height, 2*width):
.... y.extend(Y[i:i+width])
....[0, 1, 2, 6, 7, 8, 12, 13, 14]

Probably more efficient, but needs numpy:
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11],
[12, 13, 14]])
array([[ 0, 1, 2],
[ 6, 7, 8],
[12, 13, 14]])[0, 1, 2, 6, 7, 8, 12, 13, 14]

Peter
 
P

pi.arctan

One of my many project involves working with YUV-files, where I need
to reduce
the vertical resolution with a factor of two, i.e. remove every other
scan line.
Today I'm using two for-loops in the fashion shown below
y = []
for i in range(0, width*height, width*2):
for j in range(0,width):
y.append(Y[i+j])
This approach doesn't feel very pythonic but I can't come up with a
better idea to do it.
I've tried list comprehension and map together with lambda but I can't
get a flattened list
of every other scan-line...
CIF = 352x288 items for luminance and the aim is to have the list
below:
y = [0:352 704:1056 ... ]
width = 3; height = 5
Y = range(width*height)
y = []
for i in range(0, width*height, 2*width):

... y.extend(Y[i:i+width])
...>>> y

[0, 1, 2, 6, 7, 8, 12, 13, 14]

Probably more efficient, but needs numpy:

array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11],
[12, 13, 14]])>>> b = a[::2]
array([[ 0, 1, 2],
[ 6, 7, 8],
[12, 13, 14]])>>> list(b.reshape(len(b)*width))

[0, 1, 2, 6, 7, 8, 12, 13, 14]

Peter

Thanx guys!
 

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,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top