Explanation about for
Frank Millman
frank at chagford.com
Tue Jan 10 08:39:14 EST 2012
"???????? ??????" <nikos.kouras at gmail.com> wrote in message
news:afd612b7-2366-40be-badf-13c97655f72d at 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
More information about the Python-list
mailing list