what's going on here?

J

John Salerno

This might be confusing to explain, because it's a question about an
example in Beginning Python and I'll try to provide all the info I can.

First off, I'm reading a chapter on using the ReportLab modules to
create a line graph from a set of data. The first implementation of the
program uses a hard-coded list as the data source, the second
implementation pulls the data from a URL. In either case, the data is of
this format:

# year month predicted high low
2004 12 34.2 35.2 33.2
2005 01 31.5 34.5 28.5
(repeated many times)

In the first implementation, the data was a list of tuples, each tuple
being one row of the data. So grabbing the data was done like this:

pred = [row[2]-40 for row in data]
high = [row[3]-40 for row in data]
etc...

We can safely ignore the '-40', that was just for positioning. So the
variable for predicted would grab 34.2, 31.5, etc., high would get 35.2,
etc. Easy enough.

Now, the second implementation does this:

for line in urlopen(URL).readlines():
if not line.isspace() and not line[0] in COMMENT_CHARS:
data.append(map(float, line.split()))

pred = [row[2] for row in data]
high = [row[3] for row in data]
etc.

(URL is the location of the data file online. The if statement just
checks for blank lines and lines beginning with certain comment
characters, so they can be ignored.)

So finally here's my question: If you are using data.append(), doesn't
that just put all the numbers into one long list? How are the tuples
still being created in this case so that the list comprehensions still
work? It seems like there is no longer any 'row' to refer to in data.

The only thing I can think of is that each time through the for loop, a
new item (tuple or list) is being created in the data list, so that each
row of data really is being separated as its own element in the larger
list, but that doesn't seem right.

Thanks!
 
F

Felipe Almeida Lessa

Em Qui, 2006-03-16 às 16:31 +0000, John Salerno escreveu:
So finally here's my question: If you are using data.append(), doesn't
that just put all the numbers into one long list? How are the tuples
still being created in this case so that the list comprehensions still
work? It seems like there is no longer any 'row' to refer to in data.

Look the line """data.append(map(float, line.split()))"""

In other words:

# Suppose line is "2004 12 34.2 35.2 33.2"
# for our comments

# Creates a list, like ["2004", "12", "34.2", "35.2", "33.2"]
splitted = line.split()

# Convert all numbers to floats
numbers = map(float, splitted)

# It could also be
# numbers = [float(x) for x in splitted]

# Append the list to the data
# ReportLab accept both lists and tuples, so it doesn't matter
data.append(numbers)


Hope this clarifies your mind,
Felipe.
 
J

John Salerno

Felipe said:
# Suppose line is "2004 12 34.2 35.2 33.2"
# for our comments

# Creates a list, like ["2004", "12", "34.2", "35.2", "33.2"]
splitted = line.split()

Thanks guys! I think what I forgot was that split() returns a list, so
that's when the 'rows' were being created.
 
?

=?ISO-8859-1?Q?Sch=FCle_Daniel?=

[...]
So finally here's my question: If you are using data.append(), doesn't
that just put all the numbers into one long list?

no, append appends
extend does what you think

How are the tuples
still being created in this case so that the list comprehensions still
work? It seems like there is no longer any 'row' to refer to in data.

why not to fire interpreter to see what happens
>>> line1 = "1 2 3 4"
>>> line2 = "5 6 7 8"
>>> lst = []
>>> lst.append(map(float, line1.split()))
>>> lst [[1.0, 2.0, 3.0, 4.0]]
>>> lst.append(map(float, line2.split()))
>>> lst [[1.0, 2.0, 3.0, 4.0], [5.0, 6.0, 7.0, 8.0]]
>>>


hth, Daniel
 

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,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top