reordering elements of a list

Christoph Zwerschke cito at online.de
Sun Jun 4 04:06:30 CEST 2006


greenflame wrote:
> Suppose the main list is: mainlist = list('qwertyuiop')
> Suppose the ordering list is: orderinglist = [3, 4, 2, 1]
> 
> Then I am looking for a function that will take mainlist and
> orderinglist as arguments and return the following list:
> 
> ['e', 'r', 'w', 'q', 't', 'y', 'u', 'i', 'o', 'p']
> 
> Also by the way the main list is always going to be a list of strings
> and the ordering list will be a list of numbers. Also the largest
> number in orderinglist will always be equal to the length of
> orderinglist. I hope this makes any sense. Thanks for your help.

The following will do:

map(lambda c, i: i and mainlist[i-1] or c, mainlist, orderinglist)

-- Christoph



More information about the Python-list mailing list