Getting sorting order

Mark Tolonen M8R-yfto6h at mailinator.com
Mon Jun 30 11:00:02 EDT 2008


"leodp" <leodp at yahoo.com> wrote in message 
news:d1014b7c-226f-4093-b712-a41a49c4e7d9 at t54g2000hsg.googlegroups.com...
>
>> 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

How about:

>>> master=[1,4,3,2]
>>> slave1='d c b a'.split()
>>> slave2=[1,2,3,4]
>>> x=zip(master,slave1,slave2)
>>> x.sort()
>>> master,slave1,slave2=zip(*x)
>>> master
(1, 2, 3, 4)
>>> slave1
('d', 'a', 'b', 'c')
>>> slave2
(1, 4, 3, 2)

--Mark




More information about the Python-list mailing list