Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #18765

Re: Explanation about for

From Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de>
Newsgroups comp.lang.python
Subject Re: Explanation about for
Date 2012-01-10 16:22 +0100
Organization A newly installed InterNetNews server
Message-ID <jehl3f$va9$1@r03.glglgl.gl> (permalink)
References (2 earlier) <CAGeFqcC=hc5xMgwqLAbdMR=cCdjYVhAbQvS9yeaxMH=o766_mA@mail.gmail.com> <mailman.4567.1326157923.27778.python-list@python.org> <581e9a24-f948-4fc4-ae28-227d6d526780@n6g2000vbg.googlegroups.com> <jeh5jf$l7q$1@r03.glglgl.gl> <afd612b7-2366-40be-badf-13c97655f72d@o12g2000vbd.googlegroups.com>

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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