Getting sorting order

L

leodp

Hi all,

I cannot find anything on this:
I have a few lists, and would like to sort one of them (sorting-master
list).
Then I would like to sort all other lists according to how the first
one was sorted (sorting-slave lists).

Is there a standard way to do that?
From what I know sort() and sorted() do not return the order of
sorting.
Maybe I have to write some more code.

Ciao, leodp
 
P

Peter Otten

leodp said:
I cannot find anything on this:
I have a few lists, and would like to sort one of them (sorting-master
list).
Then I would like to sort all other lists according to how the first
one was sorted (sorting-slave lists).

Is there a standard way to do that?
From what I know sort() and sorted() do not return the order of
sorting.
Maybe I have to write some more code.

Or provide a better explanation and an example. Do you mean something like
this?
master = [1,3,5,2,4,6]
slave = [1,2,3,4]
slave.sort(key=master.index)
slave
[1, 3, 2, 4]

It can be made more robust and efficient with an intermediate dictionary:
master_dict = dict((v, i) for i, v in enumerate(master))
slave = ["yadda",1,2,3,6,42]
slave.sort(key=master_dict.get)
slave
['yadda', 42, 1, 3, 2, 6]

Peter
 
L

leodp

Or provide a better explanation and an example. Do you mean something like
this?

Hi Peter,
a small example:

master=[1,4,3,2]
slave1=['d','c','b','a']
slave2=[1,2,3,4]

master.sort() # this is ok, but does not return infos on how the list
was sorted
slave1.sort(key=_maybe_something_here_referring_to_master_)
slave2.sort(key=_maybe_something_here_referring_to_master_)

Then I should get:
master=[1,2,3,4]
slave1=['d','a','b','c']
slave2=[1,4,3,2]


Hope it is more clear now.
Thanks, leodp
 
P

Peter Otten

leodp said:
Or provide a better explanation and an example. Do you mean something
like this?

Hi Peter,
a small example:

master=[1,4,3,2]
slave1=['d','c','b','a']
slave2=[1,2,3,4]

master.sort() # this is ok, but does not return infos on how the list
was sorted
slave1.sort(key=_maybe_something_here_referring_to_master_)
slave2.sort(key=_maybe_something_here_referring_to_master_)

Then I should get:
master=[1,2,3,4]
slave1=['d','a','b','c']
slave2=[1,4,3,2]


Hope it is more clear now.
Thanks, leodp

You need a helper list (called master_index in the example below):
master=[1,4,3,2]
slave1=['d','c','b','a']
slave2=[1,2,3,4]
master_index = range(len(master))
master_index.sort(key=master.__getitem__)
master[:] = [master for i in master_index]
slave1[:] = [slave1 for i in master_index]
slave2[:] = [slave2 for i in master_index]
master [1, 2, 3, 4]
slave1 ['d', 'a', 'b', 'c']
slave2

[1, 4, 3, 2]


If you don't care about list object identity you can use
master = [...] instead of master[:] = [...] etc.

Peter
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top