Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #18718 > unrolled thread
| Started by | Νικόλαος Κούρας <nikos.kouras@gmail.com> |
|---|---|
| First post | 2012-01-09 15:23 -0800 |
| Last post | 2012-01-11 14:50 -0800 |
| Articles | 14 — 8 participants |
Back to article view | Back to comp.lang.python
Explanation about for Νικόλαος Κούρας <nikos.kouras@gmail.com> - 2012-01-09 15:23 -0800
Re: Explanation about for Ian Kelly <ian.g.kelly@gmail.com> - 2012-01-09 16:42 -0700
Re: Explanation about for Chris Rebert <clp2@rebertia.com> - 2012-01-09 15:58 -0800
Re: Explanation about for Ian Kelly <ian.g.kelly@gmail.com> - 2012-01-09 18:11 -0700
Re: Explanation about for Νικόλαος Κούρας <nikos.kouras@gmail.com> - 2012-01-10 01:02 -0800
Re: Explanation about for Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2012-01-10 11:57 +0100
Re: Explanation about for Νικόλαος Κούρας <nikos.kouras@gmail.com> - 2012-01-10 03:38 -0800
Re: Explanation about for Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2012-01-10 14:52 +0200
Re: Explanation about for Νικόλαος Κούρας <nikos.kouras@gmail.com> - 2012-01-10 03:37 -0800
Re: Explanation about for "Frank Millman" <frank@chagford.com> - 2012-01-10 15:39 +0200
Re: Explanation about for Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2012-01-10 16:22 +0100
Re: Explanation about for RainyDay <andrei.avk@gmail.com> - 2012-01-11 14:39 -0800
Re: Explanation about for Nick Dokos <nicholas.dokos@hp.com> - 2012-01-10 10:24 -0500
Re: Explanation about for Νικόλαος Κούρας <nikos.kouras@gmail.com> - 2012-01-11 14:50 -0800
| From | Νικόλαος Κούρας <nikos.kouras@gmail.com> |
|---|---|
| Date | 2012-01-09 15:23 -0800 |
| Subject | Explanation about for |
| Message-ID | <e1a94d90-57b4-4f6c-be9a-bef08f978639@h13g2000vbn.googlegroups.com> |
================================
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.
[toc] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2012-01-09 16:42 -0700 |
| Message-ID | <mailman.4559.1326152575.27778.python-list@python.org> |
| In reply to | #18718 |
2012/1/9 Νικόλαος Κούρας <nikos.kouras@gmail.com>: > ================================ > 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
[toc] | [prev] | [next] | [standalone]
| From | Chris Rebert <clp2@rebertia.com> |
|---|---|
| Date | 2012-01-09 15:58 -0800 |
| Message-ID | <mailman.4561.1326153525.27778.python-list@python.org> |
| In reply to | #18718 |
On Mon, Jan 9, 2012 at 3:23 PM, Νικόλαος Κούρας <nikos.kouras@gmail.com> 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/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
--
http://rebertia.com
[toc] | [prev] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2012-01-09 18:11 -0700 |
| Message-ID | <mailman.4567.1326157923.27778.python-list@python.org> |
| In reply to | #18718 |
2012/1/9 Νικόλαος Κούρας <nikos.kouras@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 | ------------------------------------------------ > 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.
[toc] | [prev] | [next] | [standalone]
| From | Νικόλαος Κούρας <nikos.kouras@gmail.com> |
|---|---|
| Date | 2012-01-10 01:02 -0800 |
| Message-ID | <581e9a24-f948-4fc4-ae28-227d6d526780@n6g2000vbg.googlegroups.com> |
| In reply to | #18727 |
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 :(
[toc] | [prev] | [next] | [standalone]
| From | Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> |
|---|---|
| Date | 2012-01-10 11:57 +0100 |
| Message-ID | <jeh5jf$l7q$1@r03.glglgl.gl> |
| In reply to | #18742 |
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
[toc] | [prev] | [next] | [standalone]
| From | Νικόλαος Κούρας <nikos.kouras@gmail.com> |
|---|---|
| Date | 2012-01-10 03:38 -0800 |
| Message-ID | <2df0f35e-8c6d-446d-8762-1ad16be3d0c8@f1g2000yqi.googlegroups.com> |
| In reply to | #18747 |
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?
[toc] | [prev] | [next] | [standalone]
| From | Jussi Piitulainen <jpiitula@ling.helsinki.fi> |
|---|---|
| Date | 2012-01-10 14:52 +0200 |
| Message-ID | <qot62gj3ejz.fsf@ruuvi.it.helsinki.fi> |
| In reply to | #18749 |
Νικόλαος Κούρας 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.
[toc] | [prev] | [next] | [standalone]
| From | Νικόλαος Κούρας <nikos.kouras@gmail.com> |
|---|---|
| Date | 2012-01-10 03:37 -0800 |
| Message-ID | <afd612b7-2366-40be-badf-13c97655f72d@o12g2000vbd.googlegroups.com> |
| In reply to | #18747 |
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?
[toc] | [prev] | [next] | [standalone]
| From | "Frank Millman" <frank@chagford.com> |
|---|---|
| Date | 2012-01-10 15:39 +0200 |
| Message-ID | <mailman.4595.1326202781.27778.python-list@python.org> |
| In reply to | #18750 |
"???????? ??????" <nikos.kouras@gmail.com> wrote in message
news:afd612b7-2366-40be-badf-13c97655f72d@o12g2000vbd.googlegroups.com...
>
> 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
[toc] | [prev] | [next] | [standalone]
| From | Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> |
|---|---|
| Date | 2012-01-10 16:22 +0100 |
| Message-ID | <jehl3f$va9$1@r03.glglgl.gl> |
| In reply to | #18750 |
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
[toc] | [prev] | [next] | [standalone]
| From | RainyDay <andrei.avk@gmail.com> |
|---|---|
| Date | 2012-01-11 14:39 -0800 |
| Message-ID | <adedbe40-ae53-4d67-ba5a-1dbddc9081e8@d8g2000vbb.googlegroups.com> |
| In reply to | #18750 |
On Jan 10, 6:37 am, Νικόλαος Κούρας <nikos.kou...@gmail.com> wrote: > 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? 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
[toc] | [prev] | [next] | [standalone]
| From | Nick Dokos <nicholas.dokos@hp.com> |
|---|---|
| Date | 2012-01-10 10:24 -0500 |
| Message-ID | <mailman.4601.1326209533.27778.python-list@python.org> |
| In reply to | #18742 |
Νικόλαος Κούρας <nikos.kouras@gmail.com> wrote: > 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 :( 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).
[toc] | [prev] | [next] | [standalone]
| From | Νικόλαος Κούρας <nikos.kouras@gmail.com> |
|---|---|
| Date | 2012-01-11 14:50 -0800 |
| Message-ID | <4ba0c650-ffc7-448d-a2f3-af8abca92812@cf6g2000vbb.googlegroups.com> |
| In reply to | #18766 |
On 10 Ιαν, 17:24, Nick Dokos <nicholas.do...@hp.com> wrote: > 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.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web