Explanation about for

  • Thread starter Íéêüëáïò Êïýñáò
  • Start date
Í

Íéêüëáïò Êïýñáò

================================
dataset = cursor.fetchall()

for row in dataset:
print ( "<tr>" )

for item in row:
print ( "<td><b><font color=yellow> %s </td>" % item )
================================

and this:

================================
dataset = cursor.fetchall()

for host, hits, agent, date in dataset:
print ( "<tr>" )

for item in host, hits, agent, date:
print ( "<td><b><font color=white> %s </td>" % item )
================================


Can you please explain me how the for structure works here?

a) In the 1st example we have 'for row in dataset' what is the value
of 'row' at that time? What part of 'dataset' is 'row'?

b) In the 2nd example we have for 'host, hits, agent, date in
dataset'. How does these 4 variables take their values out of dataset?
How dataset is being splitted?


Please explain to me if you like as simple as you can
Thank you.
 
I

Ian Kelly

2012/1/9 Íéêüëáïò Êïýñáò said:
================================
dataset = cursor.fetchall()

for row in dataset:
   print ( "<tr>" )

   for item in row:
       print ( "<td><b><font color=yellow> %s </td>" % item )
================================

and this:

================================
dataset = cursor.fetchall()

for host, hits, agent, date in dataset:
   print ( "<tr>" )

   for item in host, hits, agent, date:
       print ( "<td><b><font color=white> %s </td>" % item )
================================


Can you please explain me how the for structure works here?

You should probably read through the Python tutorial or a Python book
for the basics of Python for loops.
a) In the 1st example we have 'for row in dataset' what is the value
of 'row' at that time? What part of 'dataset' is 'row'?

dataset is an iterable object, which means that Python can ask it for
an iterator and then use that iterator to "loop" through the dataset
in some fashion. In this case, 'dataset' is a database cursor, and
the values returned by the iterator are the rows that were selected by
the query that executed, represented as tuples. 'row' takes on the
value of each of those tuples, one at a time.
b) In the 2nd example we have for 'host, hits, agent, date in
dataset'. How does these 4 variables take their values out of dataset?
How dataset is being splitted?

The second example works the same way as the first, except that
instead of storing each row tuple in a single variable called row, it
unpacks each tuple into four different variables named 'host', 'hits',
'agent', and 'date'. These represent the values of the selected
columns from the query, for each selected row.

HTH,
Ian
 
C

Chris Rebert

================================
dataset = cursor.fetchall()

for row in dataset:
   print ( "<tr>" )

   for item in row:
       print ( "<td><b><font color=yellow> %s </td>" % item )
================================

and this:

Your second snippet makes use of Python's
iterable/sequence/tuple-unpacking feature. Here's the relevant part of
the Language Reference:
http://docs.python.org/reference/simple_stmts.html#assignment-statements

By way of example, given:
seq = [1,2,3,4]
Then:
w, x, y, z = seq
Results in:
w = 1
x = 2
y = 3
z = 4
`seq` has been "unpacked", and its elements have been assigned to
variables (namely: `w`, `x`, `y`, and `z`). If the number of variables
doesn't match the number of elements, Python will raise an exception.
================================
dataset = cursor.fetchall()

for host, hits, agent, date in dataset:

for-loops perform repeated assignments to the loop variable(s), and,
like with the simple assignment statement example I gave, also permit
the use of unpacking in such assignments. To make the unpacking more
explicit, the loop can be equivalently rewritten as:
for _row in dataset:
host, hits, agent, date = _row
# rest same as before...
   print ( "<tr>" )

   for item in host, hits, agent, date:

Python's syntax for tuples is based solely on commas and does not
require parentheses, though a tuple's repr() always includes the
parentheses and programmers often/typically do too. (See
http://docs.python.org/tutorial/datastructures.html#tuples-and-sequences
..) For example:
x = 1, 2
And:
x = (1, 2)
Both do exactly the same thing: set `x` to a tuple of length 2
containing the elements 1 and 2.
The loop thus might be more clearly written as:
for item in (host, hits, agent, date):
So, what's happening is that Python is iterating over the elements of
an anonymous literal tuple; `item` will thus take on the values of
`host`, `hits`, `agent`, and `date`, in turn.
       print ( "<td><b><font color=white> %s </td>"% item )
================================

Cheers,
Chris
 
I

Ian Kelly

2012/1/9 Íéêüëáïò Êïýñáò said:
if the MySQL query was:

cursor.execute( '''SELECT host, hits, agent, date FROM visitors WHERE pin=
%s ORDER BY date DESC''', pin )

can you help me imagine how the mysql database cursor that holds the query
results would look like? I must somehow visualize it in order to understand
it!

You can think of it as a pointer, traversing over one row of the
result set at a time. Hopefully this will come out legibly:

-----------------------------------------------
| HOST | HITS | AGENT | DATE |
----------------------------------------------- -------------
| foo | 7 | IE6 | 1/1/11 | <---- | cursor |
----------------------------------------------- -------------
| bar | 42 | Firefox | 2/2/10 |
-----------------------------------------------
| baz | 4 | Chrome | 3/3/09 |
------------------------------------------------

Also what happend if the query was:
cursor.execute( '''SELECT host FROM visitors") ?

the result would have to be something likelike?

-----------------
|somehost1|
-----------------
|somehost2|
-----------------
|somehost3|
-----------------
.....................
.....................
|somehost n|
-----------------

So what values host, hits, agent, date would have in 'for host, hits,agent,
date in
 dataset' ? Every row has one string how can that be split in 4?

Why don't you try it and see what happens? But to spare you the
suspense, you would get:

ValueError: need more than 1 value to unpack

Because you can't unpack a 1-length tuple into four variables. The
code assumes that the query is selecting exactly 4 columns.
 
Í

Íéêüëáïò Êïýñáò

You can think of it as a pointer, traversing over one row of the
result set at a time.  Hopefully this will come out legibly:

-----------------------------------------------
| HOST | HITS | AGENT | DATE |
-----------------------------------------------            -------------
| foo     | 7       | IE6       |1/1/11 |   <----   | cursor |
-----------------------------------------------            -------------
| bar     | 42     | Firefox  | 2/2/10 |

Database cursor is the pointer that iterates over the result set one
row at a time?
I though that it was the name we give to the whole mysql result set
returned my cursor.execute.
Why don't you try it and see what happens?  But to spare you the
suspense, you would get:

ValueError: need more than 1 value to unpack

Because you can't unpack a 1-length tuple into four variables.  The
code assumes that the query is selecting exactly 4 columns.


-----------------------------------------------
| HOST | HITS | AGENT | DATE |
-----------------------------------------------
| foo | 7 | IE6 | 1/1/11 |
-----------------------------------------------
| bar | 42 | Firefox | 2/2/10 |
-----------------------------------------------
| baz | 4 | Chrome | 3/3/09 |
-----------------------------------------------

In this line:
for host, hits, agent, date in dataset:

'dataset' is one of the rows of the mysql result or the whole mysql
result set like the table above?

I still have trouble understanding this line :(
 
T

Thomas Rachel

Am 10.01.2012 10:02 schrieb Îικόλαος ΚοÏÏας:
-----------------------------------------------
| HOST | HITS | AGENT | DATE |
-----------------------------------------------
| foo | 7 | IE6 | 1/1/11 |
-----------------------------------------------
| bar | 42 | Firefox | 2/2/10 |
-----------------------------------------------
| baz | 4 | Chrome | 3/3/09 |
-----------------------------------------------

In this line:
for host, hits, agent, date in dataset:

'dataset' is one of the rows of the mysql result or the whole mysql
result set like the table above?

dataset is a cursor, representing the whole result set.

Iterating over it produces one row at each iteration step:

for row in dataset:
...

As each row consists of 4 fields, one iteration result is a tuple of 4
elements.

In this case,

for host, hits, agent, date in dataset:

is shorthand for

for anyunusedvariablename in dataset: # take complete row
host, hits, agent, date = anyunusedvariablename # tuple unpacking
del anyunusedvariablename # remove traces

exept that the said variable isn't created.


Thomas
 
Î

Îικόλαος ΚοÏÏας

Am 10.01.2012 10:02 schrieb Îικόλαος ΚοÏÏας:




dataset is a cursor, representing the whole result set.

Iterating over it produces one row at each iteration step:

for row in dataset:
     ...

As each row consists of 4 fields, one iteration result is a tuple of 4
elements.

In this case,

for host, hits, agent, date in dataset:

So that means that

for host, hits, agent, date in dataset:

is:

for host, hits, agent, date in (foo,7,IE6,1/1/11)

and then:

for host, hits, agent, date in (bar,42,Firefox,2/2/10)

and then:

for host, hits, agent, date in (baz,4,Chrome,3/3/09)


So 'dataset' is one row at each time?
but we said that 'dataset' represent the whole result set.
So isnt it wrong iam substituting it with one line per time only?
 
Î

Îικόλαος ΚοÏÏας

Am 10.01.2012 10:02 schrieb Îικόλαος ΚοÏÏας:




dataset is a cursor, representing the whole result set.

Iterating over it produces one row at each iteration step:

for row in dataset:
     ...

As each row consists of 4 fields, one iteration result is a tuple of 4
elements.

In this case,

for host, hits, agent, date in dataset:

So that means that

for host, hits, agent, date in dataset:

is:

for host, hits, agent, date in (foo,7,IE6,1/1/11)

and then:

for host, hits, agent, date in (bar,42,Firefox,2/2/10)

and then:

for host, hits, agent, date in (baz,4,Chrome,3/3/09)


So 'dataset' is one row at each time?
but we said that 'dataset' represent the whole result set.
So isnt it wrong iam substituting it with one line per time only?
 
J

Jussi Piitulainen

Îικόλαος ΚοÏÏας said:
So that means that

for host, hits, agent, date in dataset:

is:

for host, hits, agent, date in (foo,7,IE6,1/1/11)

and then:

for host, hits, agent, date in (bar,42,Firefox,2/2/10)

and then:

for host, hits, agent, date in (baz,4,Chrome,3/3/09)


So 'dataset' is one row at each time?
but we said that 'dataset' represent the whole result set.
So isnt it wrong iam substituting it with one line per time only?

Forget the database and meditate on simpler examples like this:
....
('a', '1')
('b', '2')
('c', '3')....
[('a', '1'), ('b', '2'), ('c', '3')]
[('a', '1'), ('b', '2'), ('c', '3')]
[('a', '1'), ('b', '2'), ('c', '3')]
Or, for that matter, even simpler examples like this:
....
a
b
c ....
abc
abc
abc

And this:

Go to the Python command line and try things out.
 
F

Frank Millman

???????? ?????? said:
So that means that

for host, hits, agent, date in dataset:

is:

for host, hits, agent, date in (foo,7,IE6,1/1/11)

and then:

for host, hits, agent, date in (bar,42,Firefox,2/2/10)

and then:

for host, hits, agent, date in (baz,4,Chrome,3/3/09)


So 'dataset' is one row at each time?
but we said that 'dataset' represent the whole result set.
So isnt it wrong iam substituting it with one line per time only?

No. 'for host, hits, agent, date in dataset:' is equivalent to -

for row in dataset: # iterate over the cursor, return a single row (tuple)
for each iteration
host, hits, agent, date = row # unpack the tuple and assign the elements
to their own names

For the first iteration, row is the tuple ('foo', 7, 'IE6', '1/1/11')
For the next iteration, row is the tuple ('bar', 42, 'Firefox', '2/2/10')
For the next iteration, row is the tuple ('baz', 4, 'Chrome', '3/3/09')

The original line uses a python technique that combines these two lines into
one.

HTH

Frank Millman
 
T

Thomas Rachel

Am 10.01.2012 12:37 schrieb Îικόλαος ΚοÏÏας:
So that means that

for host, hits, agent, date in dataset:

is:

for host, hits, agent, date in (foo,7,IE6,1/1/11)

and then:

for host, hits, agent, date in (bar,42,Firefox,2/2/10)

and then:

for host, hits, agent, date in (baz,4,Chrome,3/3/09)

No.

As said, dataset is the whole result set. For now, you can see it as a
list of all rows (which you get if you do l=list(dataset)).

Let's assume you have your data in a list now, which is equivalent
concerning the iteration.

Then you have something like

dataset = [
('foo',7,'IE6','1/1/11'),
('bar',42,'Firefox','2/2/10'),
('baz',4,'Chrome','3/3/09')
]


Doing

for row in dataset: print row

is equivalent to

row = ('foo',7,'IE6','1/1/11')
print row

row = ('bar',42,'Firefox','2/2/10')
print row

row = ('baz',4,'Chrome','3/3/09')
print row



Doing

for a, b, c, d in dataset:
do_funny_stuff(a, d, c, b)

is

a, b, c, d = ('foo',7,'IE6','1/1/11');
# which is the same as
# a = 'foo'; b = 7; c = 'IE6'; d = '1/1/11';
do_funny_stuff(a, d, c, b)

a, b, c, d = ('bar',42,'Firefox','2/2/10')
do_funny_stuff(a, d, c, b)

a, b, c, d = ('baz',4,'Chrome','3/3/09')
do_funny_stuff(a, d, c, b)


The "body" of the for suite is executed once for each element.

You have read already

http://docs.python.org/tutorial/controlflow.html#for-statements
http://docs.python.org/library/stdtypes.html#iterator-types

?


Thomas
 
N

Nick Dokos

Îικόλαος ΚοÏÏας said:
Database cursor is the pointer that iterates over the result set one
row at a time?
I though that it was the name we give to the whole mysql result set
returned my cursor.execute.



-----------------------------------------------
| HOST | HITS | AGENT | DATE |
-----------------------------------------------
| foo | 7 | IE6 | 1/1/11 |
-----------------------------------------------
| bar | 42 | Firefox | 2/2/10 |
-----------------------------------------------
| baz | 4 | Chrome | 3/3/09 |
-----------------------------------------------

In this line:
for host, hits, agent, date in dataset:

'dataset' is one of the rows of the mysql result or the whole mysql
result set like the table above?

I still have trouble understanding this line :(

You can think of it as a list of tuples. Forget about cursors and
databases for now. If l is a list [1, 2, 3, 4] you iterate over it like
this:

for x in l:
print x

and you get each element of the list[fn:1]. Similarly if l is a list of tuples
l = [(1, 2, 3, 4), (5, 6, 7, 8), (9, 10, 11, 12)] you can iterate over the
list:

for x in l:
print x

In this case, x is going to be (1,2,3,4) the first time through the loop, (5,6,7,8)
the second time and so on. You can break each x apart within the loop:

for x in l:
t1, t2, t3, t4 = x
print x, t1, t2, t3, t4

or you can break it apart like this - it's essentially the same thing:

for t1, t2, t3, t4 in l:
print t1, t2, t3, t4

You have been encouraged repeatedly to try these things interactively:
please do so with the above examples and all will become clear.

Going back to cursors and databases: you *can* think of 'dataset' as
being a list of tuples - a list of all the query results, but with one
proviso. The difference when you use a cursor is that `dataset' may
be a lazy list (an "iterator"): instead of the whole set of results
being in memory at the same time, the system will take care of doing
whatever is necessary to get more of the results when it needs them. But
the behavior is the *same* in the sense that the output that you get is
the same (you will only see differences if you are keeping an eye on how
much memory and how much time your program is using).

Nick

Footnotes:

[fn:1] ... and, no, you *can't express* this as
"the first time it is

for x in 1:
...

and the second time it is

for x in 2:
...

" as you asked in another email. That's arrant nonsense: x takes
successive values in the list l for every iteration of the for
loop. This is elementary Python (nay, elementary programming, period).
 
R

RainyDay

So that means that

for host, hits, agent, date in dataset:

is:

for host, hits, agent, date in  (foo,7,IE6,1/1/11)

and then:

for host, hits, agent, date in  (bar,42,Firefox,2/2/10)

and then:

for host, hits, agent, date in  (baz,4,Chrome,3/3/09)

So 'dataset' is one row at each time?
but we said that 'dataset' represent the whole result set.
So isnt it wrong iam substituting it with one line per time only?


It maps naturally to a phrase 'for page in a book'. Book refers
to a complete book with all pages, but 'for page in a book' refers
to each page, one by one. 'for each page in book: turn it'. The
meaning is to turn each page, so your question is equivalent
to asking 'so book is one page at each time'? No, it is not
and doesn't have to be (unless it's a really short book!)

-ak
 
Î

Îικόλαος ΚοÏÏας

You have been encouraged repeatedly to try these things interactively:
please do so with the above examples and all will become clear.

I tried all those examples you provided me in IDLE and finally graps
they idea behind it.

Thanks for all your detailed explanations and the patience you showed
me until i finally understood this concept.
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top