sorting two corresponding lists?

邓弈龙 yilongdeng at gmail.com
Tue Apr 21 11:14:59 EDT 2009


solution by jadec.seven at gmail.com<mhtml:{154E1F55-D942-4CE9-9B8C-82060E1E80CE}mid://00000047/!x-usc:mailto:jadec.seven@gmail.com>,
China

values,items = zip( *sorted( zip (values,items)))

----- Original Message -----
From: "Esmail" <ebonak at hotmail.com<mhtml:{154E1F55-D942-4CE9-9B8C-82060E1E80CE}mid://00000047/!x-usc:mailto:ebonak@hotmail.com>
>
To: <python-list at python.org<mhtml:{154E1F55-D942-4CE9-9B8C-82060E1E80CE}mid://00000047/!x-usc:mailto:python-list@python.org>
>
Sent: Tuesday, April 21, 2009 12:10 AM
Subject: sorting two corresponding lists?


> Hello all,
>
> I wonder if someone could help me with sorting two corresponding lists.
>
> For instance the first list contains some items, and the second list
> contains their value (higher is better)
>
> items = [apple, car, town, phone]
> values = [5, 2, 7, 1]
>
> I would like to sort the 'items' list based on the 'values' list so
> that I end up with the following two list:
>
> items = [town, apple, car, phone]
> values = [7, 5, 2, 1]
>
> So I would like to keep the corresponding value still corresponding
> after the sorting.
>
> Is there an easy/nice/Pythonic way to do this?
>
> Thanks,
> Esmail
>
> --
> http://mail.python.org/mailman/listinfo/python-list<mhtml:{154E1F55-D942-4CE9-9B8C-82060E1E80CE}mid://00000047/!x-usc:http://mail.python.org/mailman/listinfo/python-list>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090421/dc68c8a9/attachment-0001.html>


More information about the Python-list mailing list