Anagram
Jonathan Hogg
jonathan at onegoodidea.com
Thu Jan 24 09:34:34 EST 2002
On 24/1/2002 12:00, in article a2ot19$hg4$1 at serv1.iunet.it, "Alex Martelli"
<aleax at aleax.it> wrote:
> Not sure what you mean. "get the idx1-th element
> from list llis and remove it from the list too"
> is llis.pop(idx1); is that what you mean by
> "comprehend"?
No, I think he meant "make it into a list comprehension". Perhaps:
llis = [ x for i, x in zip(xrange(len(lis)), lis) if i <> idx1 ]
Though this is substantially uglier than the original version. It can be
slightly improved in readability with a suitable definition for an indexed
zip (a common operation in functional programming):
def indexed( items, from=0 ):
index = from
for item in items:
yield index, item
index += 1
llis = [ x for i, x in indexed(llis) if i <> idx1 ]
[Has one of these made it into the standard library yet? I know it's a
trivial function, but I think common functions should have a standard name
and exist in a standard place for readability - somewhat related to the
separate factorial discussion going on.]
Jonathan
More information about the Python-list
mailing list