473,480 Members | 3,017 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

extract multiple ranges from a list

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
Mar 8 '08 #1
3 2039
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
Mar 8 '08 #2
pi*******@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

Mar 8 '08 #3
On 8 Mar, 17:32, Peter Otten <__pete...@web.dewrote:
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!
Mar 8 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1711
by: Ben O'Steen | last post by:
Scenario: ========= Using PyGame in particular, I am trying to write an application that will run a scripted timeline of events, eg at 5.5 seconds, play xxx.mp3 and put the image of a ball on...
1
3332
by: poohnie08 | last post by:
i have a excel spreadsheet showing staff name, date,work hour, ot hour, slot1, slot2, slot3, slot4 and others). The "()" will keep repeating from day 1 until end of month. eg in excel spreadsheet,...
2
2002
by: junkaccount | last post by:
Hello, Using Access 2000 I would like to create one report that returns sales data for various date ranges input by the user i.e. weekly, monthly, etc. This report is bound to a query that pulls...
5
8763
by: John Brock | last post by:
I am using VB.NET to read Excel workbooks which have various named ranges, some of which may not exist in any given workbook. I am trying to get a list of all the range names -- otherwise I need...
67
7598
by: PC Datasheet | last post by:
Transaction data is given with date ranges: Beginning End 4/1/06 4/4/06 4/7/06 4/11/06 4/14/06 4/17/06 4/18/06 4/21/06 426/06 ...
35
9296
by: keerthyragavendran | last post by:
hi i'm downloading a single file using multiple threads... how can i specify a particular range of bytes alone from a single large file... for example say if i need only bytes ranging from...
10
2365
by: kyosohma | last post by:
Hi, I am working on a timesheet application in which I need to to find the first pay period in a month that is entirely contained in that month to calculate vacation time. Below are some example...
16
7218
by: Alex30093 | last post by:
OK This is my 1st post to any forum, but I rely on forums all the time. Short story is I find myself needing to become a Access DBA for work. Situation: I want to use one table of events that...
0
1596
by: =?ISO-8859-1?Q?J=F6rg_Battermann?= | last post by:
Hello there, Is there any -reliable- way to extract certain, given ranges in excel files into either a picture (each) or into proper xhtml? I am asking because I am currently automating a...
0
7060
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7022
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5365
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4799
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3013
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3004
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1311
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
206
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.