List transormation

Andy Smith ams70 at cam.ac.uk
Tue Sep 19 12:40:17 EDT 2000


"Dale Strickland-Clark" <dale at out-think.NOSPAMco.uk> writes:

> Before I waste too much time on this, reinventing the wheel, is there a
> quick way to reorder the items in a list under the control of another list?
> 
> e.g..
> 
> x = ['kipper', 'my', 'a', 'nose', 'is']
> y = [1, 3, 4, 2, 0]
> 
> y transforms the order of x to produce:
> 
> ['my', 'nose', 'is', 'a', 'kipper']

>>> map (lambda n: x[n], y)
['my', 'nose', 'is', 'a', 'kipper']

This could be wrapped into a function taking arguments equivalent to x
and y if you wanted to use it a lot.

    Andy
-- 
Andy Smith                                             ams70 at cam.ac.uk
St John's College, Cambridge, CB2 1TP, UK               (07989) 453208



More information about the Python-list mailing list