Explanation about for

Jussi Piitulainen jpiitula at ling.helsinki.fi
Tue Jan 10 07:52:32 EST 2012


Νικόλαος Κούρας 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.



More information about the Python-list mailing list