transforming a list into a string
Duncan Booth
me at privacy.net
Mon Aug 2 05:11:49 EDT 2004
Christopher T King <squirrel at WPI.EDU> wrote in
news:Pine.LNX.4.44.0408011814020.21160-100000 at ccc4.wpi.edu:
> On Sat, 31 Jul 2004, Tim Peters wrote:
>
>> Absolutely. Note that Peter Otten previously posted a lovely O(N)
>> solution in this thread, although it may be too clever for some
>> tastes:
>>
>> >>> from itertools import izip
>> >>> items = ['1','2','7','8','12','13']
>> >>> it = iter(items)
>> >>> ",".join(["{%s,%s}" % i for i in izip(it, it)])
>> '{1,2},{7,8},{12,13}'
>> >>>
>
> A bit too clever for mine, mostly because neither izip() nor zip() is
> guaranteed to process its arguments in a left-to-right order (although
> there's no reason for them not to).
You should read an earlier thread on this topic:
http://groups.google.co.uk/groups?threadm=c91pft%24rl0%2404%241%40news.t-online.com
I make exactly that point, that the order isn't guaranteed, and was
refuted fairly convincingly by Peter Otten. The documentation says that
izip is equivalent to a particular reference implementation. Any
implementation which didn't preserve the left to right ordering wouldn't
match the reference:
Peter Otten wrote:
>> Passing the same iterator multiple times to izip is a pretty neat
>> idea, but I would still be happier if the documentation explicitly
>> stated that it consumes its arguments left to right.
>
> From the itertools documentation:
>
> """
> izip(*iterables)
>
> Make an iterator that aggregates elements from each of the iterables.
> Like zip() except that it returns an iterator instead of a list. Used
> for lock-step iteration over several iterables at a time. Equivalent
> to:
>
> def izip(*iterables):
> iterables = map(iter, iterables)
> while iterables:
> result = [i.next() for i in iterables]
> yield tuple(result)
> """
>
> I'd say the "Equivalent to [reference implementation]" statement
> should meet your request.
More information about the Python-list
mailing list