reordering elements of a list

G

greenflame

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.
 
T

Travis E. Oliphant

greenflame said:
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 element-based
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
 
R

Roberto Bonvallet

3 Jun 2006 17:46:49 -0700 said:
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.
 
C

Christoph Zwerschke

greenflame said:
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
 
G

greenflame

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. :)
 
R

Robert Kern

greenflame said:
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/built-in-funcs.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
 
R

Roberto Bonvallet

greenflame said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top