473,320 Members | 2,000 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

reordering elements of a list

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.

Jun 4 '06 #1
7 7622
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 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

Jun 4 '06 #2
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
Jun 4 '06 #3
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
Jun 4 '06 #4
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. :)

Jun 4 '06 #5
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/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

Jun 4 '06 #6
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
Jun 5 '06 #7
Thanks all for your help!

Jun 5 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: tgh003 | last post by:
So lets assume I have a list of tasks in db table (table looks like: ID, task, sort) And I want to reorder the task list without updating all the records in the table. I dont want to run a sql...
11
by: Laszlo Zsolt Nagy | last post by:
Hello, Do you know how to implement a really efficient self reordering list in Python? (List with a maximum length. When an item is processed, it becomes the first element in the list.) I would...
2
by: greenflame | last post by:
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.