anonymous assignment

Terry Reedy tjreedy at udel.edu
Tue May 13 03:16:41 EDT 2008


"Arnaud Delobelle" <arnodel at googlemail.com> wrote in message 
news:m2lk2eivos.fsf at googlemail.com...
| "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

and in order, I wrote once, but seem to have erased in revising (frown)

| >     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!







More information about the Python-list mailing list