Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Explanation about for

Reply
Thread Tools

Explanation about for

 
 
Guest
Posts: n/a
 
      01-09-2012
================================
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.
 
Reply With Quote
 
 
 
 
Ian Kelly
Guest
Posts: n/a
 
      01-09-2012
2012/1/9 <>:
> ================================
> 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
 
Reply With Quote
 
 
 
 
Chris Rebert
Guest
Posts: n/a
 
      01-09-2012
On Mon, Jan 9, 2012 at 3:23 PM, Νικόλαος Κούρας <> wrote:
> ================================
> 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/sim...ent-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/data...-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
--
http://rebertia.com
 
Reply With Quote
 
Ian Kelly
Guest
Posts: n/a
 
      01-10-2012
2012/1/9 <>:
> 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.
 
Reply With Quote
 
Guest
Posts: n/a
 
      01-10-2012
On 10 Ιαν, 03:11, Ian Kelly <ian.g.ke...@gmail.com> wrote:
> 2012/1/9 Íéêüëáïò Êïýñáò <nikos.kou...@gmail.com>:
>
> > 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 |
> ------------------------------------------------


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.

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



-----------------------------------------------
| 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
 
Reply With Quote
 
Thomas Rachel
Guest
Posts: n/a
 
      01-10-2012
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
 
Reply With Quote
 
Νικόλαος Κούρας
Guest
Posts: n/a
 
      01-10-2012
On 10 Ιαν, 12:57, Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-
a470-7603bd3aa...@spamschutz.glglgl.de> wrote:
> 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:


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?
 
Reply With Quote
 
Νικόλαος Κούρας
Guest
Posts: n/a
 
      01-10-2012
On 10 Ιαν, 12:57, Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-
a470-7603bd3aa...@spamschutz.glglgl.de> wrote:
> 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:


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?
 
Reply With Quote
 
Jussi Piitulainen
Guest
Posts: n/a
 
      01-10-2012
Νικόλαος Κούρας writes:

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

>>> xy = zip("abc", "123")
>>> for x, y in xy: print(x, y)

....
('a', '1')
('b', '2')
('c', '3')
>>> for x, y in xy: print(xy)

....
[('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:

>>> bag = "abc"
>>> for x in bag: print(x)

....
a
b
c
>>> for x in bag: print(bag)

....
abc
abc
abc
>>>


And this:

>>> a, b, c = bag
>>> a, b, c

('a', 'b', 'c')
>>> bag

'abc'
>>>


Go to the Python command line and try things out.
 
Reply With Quote
 
Frank Millman
Guest
Posts: n/a
 
      01-10-2012

"???????? ??????" <> wrote in message
news:afd612b7-2366-40be-badf-...
>
> 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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple Explanation to Networking Wirelessly?? Jaxim Wireless Networking 4 08-19-2005 05:04 AM
explanation Mariusz VHDL 1 01-13-2004 02:10 AM
Debug message explanation Dean Cisco 1 01-07-2004 12:38 PM
DVMRP Explanation Acer0001 Cisco 0 11-20-2003 04:41 AM
Need Explanation Kaladhaur Palaniappa Perl 0 08-07-2003 09:47 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57