Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #18720
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder2.hal-mli.net!news.stack.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <ian.g.kelly@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.003 |
| X-Spam-Evidence | '*H*': 0.99; '*S*': 0.00; 'fashion.': 0.07; 'python': 0.08; 'host,': 0.09; 'query,': 0.09; 'rows': 0.09; 'storing': 0.09; 'tuple': 0.09; 'columns': 0.16; 'executed,': 0.16; 'iterator': 0.16; 'row': 0.16; 'row.': 0.16; 'tuples,': 0.16; '\xa0for': 0.16; '\xa0print': 0.16; 'cc:addr:python-list': 0.16; 'received:74.125.82.44': 0.16; 'received:mail-ww0-f44.google.com': 0.16; 'this:': 0.16; 'cc:no real name:2**0': 0.20; 'charset:iso-8859-7': 0.21; 'header:In-Reply-To:1': 0.22; 'here?': 0.23; 'cc:2**0': 0.24; 'object,': 0.24; 'structure': 0.26; 'variable': 0.28; 'message-id:@mail.gmail.com': 0.28; 'date:': 0.29; 'cc:addr:python.org': 0.29; 'second': 0.29; 'example': 0.29; 'does': 0.32; 'values': 0.32; 'instead': 0.33; 'named': 0.33; 'probably': 0.34; 'received:74.125.82': 0.35; 'explain': 0.36; 'basics': 0.37; 'query': 0.37; 'variables': 0.37; 'except': 0.37; 'received:74.125': 0.37; 'received:google.com': 0.37; '2nd': 0.38; 'some': 0.38; 'returned': 0.39; 'represent': 0.39; 'should': 0.39; 'being': 0.39; 'called': 0.40; 'selected': 0.68; 'skip:= 30': 0.68; '1st': 0.70; 'time?': 0.73; 'row,': 0.84; 'agent,': 0.91 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; bh=ykiCMvbKI0A8QYbm+Ru7sf9TFqRmduWPOLLblqoCedk=; b=az/4nkEfiSN2Bo05stBs9e+t8uizKo8B7XxbC0HMySsHSu0Vp1ngBOIMIoST5nUpW5 SiC07ob0l34BSoojQ/JNmUWphzh7BZV/wWsYoKrepEewVTJt5PxB28smpyQBNWiB7c2b E1Kik45gMJq/eABB2/AQKzsNrnTZrOtaFMnXo= |
| MIME-Version | 1.0 |
| In-Reply-To | <e1a94d90-57b4-4f6c-be9a-bef08f978639@h13g2000vbn.googlegroups.com> |
| References | <e1a94d90-57b4-4f6c-be9a-bef08f978639@h13g2000vbn.googlegroups.com> |
| From | Ian Kelly <ian.g.kelly@gmail.com> |
| Date | Mon, 9 Jan 2012 16:42:22 -0700 |
| Subject | Re: Explanation about for |
| To | Νικόλαος Κούρας <nikos.kouras@gmail.com> |
| Content-Type | text/plain; charset=ISO-8859-7 |
| Content-Transfer-Encoding | quoted-printable |
| Cc | python-list@python.org |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.12 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.4559.1326152575.27778.python-list@python.org> (permalink) |
| Lines | 56 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1326152575 news.xs4all.nl 6849 [2001:888:2000:d::a6]:47129 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.python:18720 |
Show key headers only | View raw
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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
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
csiph-web