[Tutor] Why are these results different?
alan.gauld at btinternet.com
Thu Nov 19 09:57:30 CET 2009
"Stephen Nelson-Smith" <sanelson at gmail.com> wrote
> I'm seeing different behaviour between code that looks to be the same.
> It obviously isn't the same, so I've misunderstood something:
In the first instance the two for-loops are inside the chain() call.
In the second you apply the chain inside the loops, so it only
applies to one item at a time.
>>>> logs = itertools.chain.from_iterable(glob.glob('%sded*/%s*%s.gz' %
>>>> (source_dir, log, date)) for log in log_names for date in log_dates)
>>>> for log in logs:
> ... print log
> /Volumes/UNTITLED 1/ded1/access_log-20091105.gz
> /Volumes/UNTITLED 1/ded2/access_log-20091105.gz
> for date in log_dates:
> for log in log_names:
> logs = itertools.chain.from_iterable(glob.glob('%sded*/%s*%s.gz'
> % (source_dir, log, date)))
> Gives me one character at a time when I iterate over logs.
The final result is whatever chain() evaluated for the final loop
Apparently a string.
Author of the Learn to Program web site
More information about the Tutor