Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'pointer': 0.05; 'python': 0.08; 'foo': 0.09; 'hits': 0.09; 'host,': 0.09; 'iterate': 0.09; 'loop.': 0.09; 'rows': 0.09; 'successive': 0.09; 'tuple': 0.09; 'valueerror:': 0.09; 'variables.': 0.09; 'essentially': 0.10; 'output': 0.10; 'encouraged': 0.12; '*can*': 0.16; 'above?': 0.16; 'baz': 0.16; 'clear.': 0.16; 'cursor': 0.16; 'databases:': 0.16; 'iterates': 0.16; 'like?': 0.16; 'nick': 0.16; 'pointer,': 0.16; 'row': 0.16; 'unpack': 0.16; 'cc:addr:python-list': 0.16; 'this:': 0.16; 'examples': 0.16; 'wrote:': 0.18; 'memory': 0.21; 'holds': 0.21; 'asked': 0.22; 'header:In-Reply-To:1': 0.22; 'assumes': 0.23; 'loop,': 0.23; 'similarly': 0.23; 'somehow': 0.23; 'string': 0.24; 'code': 0.25; '"the': 0.26; '(you': 0.28; 'repeatedly': 0.28; 'print': 0.29; 'cc:addr:python.org': 0.29; 'second': 0.29; 'host': 0.30; 'kelly': 0.30; 'line:': 0.30; 'tuples': 0.30; 'list:': 0.31; 'values': 0.32; 'list': 0.32; 'break': 0.32; "can't": 0.32; 'instead': 0.33; 'programming,': 0.34; 'things': 0.34; 'trouble': 0.35; 'something': 0.35; 'header:Received:8': 0.36; '...': 0.36; 'cc:2**1': 0.36; 'element': 0.37; 'query': 0.37; 'but': 0.37; 'another': 0.37; 'think': 0.37; 'skip:- 40': 0.37; 'doing': 0.38; 'skip:\xce 10': 0.38; 'received:org': 0.38; 'returned': 0.39; 'being': 0.39; 'why': 0.39; 'help': 0.39; "it's": 0.40; 'difference': 0.40; 'within': 0.60; 'more': 0.61; 'your': 0.61; 'back': 0.62; 'order': 0.62; 'results': 0.63; 'agent': 0.65; '11,': 0.68; '8bit%:100': 0.69; 'become': 0.69; 'care': 0.71; 'imagine': 0.71; 'reply-to:no real name:2**0': 0.72; 'time?': 0.73; 'skip:\xc2 10': 0.74; 'visitors': 0.77; 'header :Reply-to:1': 0.84; 'chrome': 0.84; 'skip:\xc3 10': 0.84; 'suspense,': 0.84; 'agent,': 0.91 X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "Cc" To: =?us-ascii?Q?=3D=3FISO-8859-7=3FB=3Fzenq=2FOvh7=2FIgyu=2F98eHy=3F=3D?= From: Nick Dokos Subject: Re: Explanation about for In-Reply-To: Message from =?us-ascii?Q?=3D=3FISO-8859-7=3FB=3Fzenq=2FOvh7?= =?us-ascii?Q?=2FIgyu=2F98eHy=3F=3D?= of "Tue\, 10 Jan 2012 01\:02\:59 PST." <581e9a24-f948-4fc4-ae28-227d6d526780@n6g2000vbg.googlegroups.com> References: <581e9a24-f948-4fc4-ae28-227d6d526780@n6g2000vbg.googlegroups.com> X-Mailer: MH-E 8.2; nmh 1.2; GNU Emacs 23.2.50 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Tue, 10 Jan 2012 10:24:10 -0500 Sender: nicholas.dokos@hp.com Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list Reply-To: nicholas.dokos@hp.com List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 150 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1326209533 news.xs4all.nl 6901 [2001:888:2000:d::a6]:59225 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:18766 =CE=9D=CE=B9=CE=BA=CF=8C=CE=BB=CE=B1=CE=BF=CF=82 =CE=9A=CE=BF=CF=8D=CF=81= =CE=B1=CF=82 wrote: > On 10 =CE=99=CE=B1=CE=BD, 03:11, Ian Kelly wrote: > > 2012/1/9 =C3=8D=C3=A9=C3=AA=C3=BC=C3=AB=C3=A1=C3=AF=C3=B2 =C3=8A=C3=AF= =C3=BD=C3=B1=C3=A1=C3=B2 : > > > > > if the MySQL query was: > > > > > cursor.execute( '''SELECT host, hits, agent, date FROM visitors WHERE= pin =3D > > > %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 unde= rstand > > > it! > > > > You can think of it as a pointer, traversing over one row of the > > result set at a time. =C2=A0Hopefully this will come out legibly: > > > > ----------------------------------------------- > > | HOST | HITS | AGENT | DATE | > > ----------------------------------------------- =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0------------- > > | foo =C2=A0 =C2=A0 | 7 =C2=A0 =C2=A0 =C2=A0 | IE6 =C2=A0 =C2=A0 =C2=A0= | 1/1/11 | =C2=A0 <---- =C2=A0 | cursor | > > ----------------------------------------------- =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0------------- > > | bar =C2=A0 =C2=A0 | 42 =C2=A0 =C2=A0 | Firefox =C2=A0| 2/2/10 | > > ----------------------------------------------- > > | baz =C2=A0 =C2=A0| 4 =C2=A0 =C2=A0 =C2=A0 | Chrome | 3/3/09 | > > ------------------------------------------------ >=20 > 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. >=20 > > > > > > > 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=C2=A0host, hits, agent, date would have in 'for=C2=A0h= ost, hits, agent, > > > date in > > > =C2=A0dataset' ? Every row has one string how can that be split in 4? > > > > Why don't you try it and see what happens? =C2=A0But 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. =C2=A0The > > code assumes that the query is selecting exactly 4 columns. >=20 >=20 > ----------------------------------------------- > | HOST | HITS | AGENT | DATE | > ----------------------------------------------- > | foo | 7 | IE6 | 1/1/11 | > ----------------------------------------------- > | bar | 42 | Firefox | 2/2/10 | > ----------------------------------------------- > | baz | 4 | Chrome | 3/3/09 | > ----------------------------------------------- >=20 > In this line: > for host, hits, agent, date in dataset: >=20 > 'dataset' is one of the rows of the mysql result or the whole mysql > result set like the table above? >=20 > 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 tup= les l =3D [(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 =3D 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).