I am trying to reorder elements of a list and I am stuck as to what
might be the best way to approach this. I have a (main) list of
elements and another (ordering) list (which is may shorter, but not
longer than the main list) which contains the order in which I want the
elements of the main list but only as far along as the length of the
ordering list. This may be confusing so I will try to give an example.
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. 7 7367
greenflame wrote: I am trying to reorder elements of a list and I am stuck as to what might be the best way to approach this. I have a (main) list of elements and another (ordering) list (which is may shorter, but not longer than the main list) which contains the order in which I want the elements of the main list but only as far along as the length of the ordering list. This may be confusing so I will try to give an example.
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.
NumPy ( http://numeric.scipy.org ) can do this using elementbased
indexing on an array of strings.
Your example:
import numpy
a = numpy.array('qwertyuiop','c')
newlist = a[[2,3,1,0]+range(4,10)].tolist()
print newlist
Returns:
['e', 'r', 'w', 'q', 't', 'y', 'u', 'i', 'o', 'p']
But you can also do it with list comprehension pretty easily, so this is
probably just a shameless plug for NumPy :)
Travis
3 Jun 2006 17:46:49 0700, greenflame <al*********@yahoo.com>: 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'] mainlist = list('qwertyuiop') orderinglist = [3, 4, 2, 1] [mainlist[i  1] for i in orderinglist] + mainlist[len(orderinglist):]
['e', 'r', 'w', 'q', 't', 'y', 'u', 'i', 'o', 'p']
Best regards.

Roberto Bonvallet
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[i1] or c, mainlist, orderinglist)
 Christoph
Thank you all for your replies. The only thing is I do not understand
how the code is working. The following are more particular questions.
Travis: Iam sorry, but I do not know what list comprehension is.
Roberto: I do not understand the first half of the last line of your
code. Also thank you for also teaching me to use '+' to append one list
to another. This will be very useful for me.
Christoph: I do not undertand the map method.
Thanks again for all the help. :)
greenflame wrote: Thank you all for your replies. The only thing is I do not understand how the code is working. The following are more particular questions.
Actually, these are statements, not questions. But anyways:
Travis: Iam sorry, but I do not know what list comprehension is. Roberto: I do not understand the first half of the last line of your code.
That's what a list comprehension is.
Also thank you for also teaching me to use '+' to append one list to another. This will be very useful for me. Christoph: I do not undertand the map method.
There is documentation on map() about halfway down this page: http://docs.python.org/lib/builtinfuncs.html
Here's a tutorial which you should read: http://docs.python.org/tut/tut.html

Robert Kern
"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
 Umberto Eco
greenflame <al*********@yahoo.com>: Roberto: I do not understand the first half of the last line of your code.
[mainlist[i  1] for i in orderinglist] is a list made with the
elements of orderinglist, but instead of taking the actual value i
from the list, the value that is taken is mainlist[i  1].
If orderinglist is [3, 4, 2, 1], then [mainlist[i  1] for i in
orderinglist] is:
[mainlist[3  1], mainlist[4  1], mainlist[2  1], mainlist[1  1]]
Remember that indexing starts from 0.

Roberto Bonvallet
Thanks all for your help! This discussion thread is closed Replies have been disabled for this discussion. Similar topics
10 posts
views
Thread by tgh003 
last post: by

11 posts
views
Thread by Laszlo Zsolt Nagy 
last post: by

2 posts
views
Thread by greenflame 
last post: by
          