anonymous assignment

Arnaud Delobelle arnodel at googlemail.com
Tue May 13 02:07:15 EDT 2008


"Terry Reedy" <tjreedy at udel.edu> writes:

> "Arnaud Delobelle" <arnodel at googlemail.com> wrote in message 
> news:c689625d-b2d2-4268-ae40-15e3c9b78ea2 at x41g2000hsb.googlegroups.com...
>
> here is a very sophisticated implementation :)
>
>>>> def extract(indices, seq):
> ...     return tuple(seq[i] for i in indices)
> ...
>>>> y, d = extract((0, 2), time.localtime())
>>>> y, d
> (2008, 12)
>
> ===================
> Or a generator version:
>
> # 3.0
> def extract(iterable, indexes):
>     # assume indexes are all legal
>     enext = enumerate(iterable).__next__
>     i,item = enext()
>     for index in indexes:
>         while i < index:
>             i,item = enext()
>         yield item
>
> import time
> print(list(extract(time.localtime(), (0,2))))
>
> #prints [2008, 12] 

but extract('python', (3, 1)) won't work!

-- 
Arnaud



More information about the Python-list mailing list