[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

extract multiple ranges from a list

pi.arctan

3/8/2008 3:29:00 PM

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
3 Answers

Paul Hankin

3/8/2008 3:42:00 PM

0

On Mar 8, 3:28 pm, pi.arc...@gmail.com wrote:
> 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))

--
Paul Hankin

Peter Otten

3/8/2008 4:32:00 PM

0

pi.arctan@gmail.com wrote:

> 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:

>>> import numpy
>>> width = 3
>>> height = 5
>>> Y = range(width*height)
>>> a = numpy.array(Y).reshape(height, width)
>>> a
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11],
[12, 13, 14]])
>>> b = a[::2]
>>> b
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

pi.arctan

3/8/2008 10:28:00 PM

0

On 8 Mar, 17:32, Peter Otten <__pete...@web.de> wrote:
> pi.arc...@gmail.com wrote:
> > 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:
>
> >>> import numpy
> >>> width = 3
> >>> height = 5
> >>> Y = range(width*height)
> >>> a = numpy.array(Y).reshape(height, width)
> >>> a
>
> array([[ 0, 1, 2],
> [ 3, 4, 5],
> [ 6, 7, 8],
> [ 9, 10, 11],
> [12, 13, 14]])>>> b = a[::2]
> >>> b
>
> 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!