transforming a list into a string
Roy Smith
roy at panix.com
Sat Jul 31 21:36:43 EDT 2004
Andrew Bennetts <andrew-pythonlist at puzzling.org> wrote:
> It's a two-liner, not a one-liner (although it could be made into a
> one-liner with enough contortions...).
>
> The only other way I could see to expand this solution would be to write it
> as:
> it = iter(items)
> pairs = izip(it, it)
> s = ",".join(["{%s,%s}" % i for i in pairs])
>
> I don't know if three-liners meet your threshold for verbosity ;)
It's better, but I'd unroll it one more step and write:
it = iter (items)
pairs = izip (it, it)
strings = ["{%s,%s}" % i for i in pairs]
s = ",".join (strings)
I'm also not particularly happy about the choice of "it" as a variable
name. The "izip (it, it)" construct makes me think of Dr. Evil :-)
> I find Peter's original form easy to read -- if you understand how "izip(it,
> it)" works (which is a very simple and elegant way to iterate over (it[n],
> it[n+1]) pairs), the rest is very clear.
It's not the izip bit that bothers me in the original, it's the deeply
nested construct of
",".join(["{%s,%s}" % i for i in izip(it, it)])
There's too much going on in that one line to get your head around
easily. I suppose people who are really into functional programming
might find it understandable, but I find it rather obtuse.
More information about the Python-list
mailing list