sorting two corresponding lists?

tiefeng wu icebergwtf at gmail.com
Tue Apr 21 18:10:09 CEST 2009


>
> Esmail wrote:
>
> > 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]


My solution, I know the 'zip' version is more elegant, this is just for
fun:)

>>> items = ['apple', 'car', 'town', 'phone']
>>> values = [5, 2, 7, 1]
>>> new_values = sorted(values, reverse = True)
>>> new_items = [items[x] for x in [i for i in map(values.index,
new_values)]]
>>> print(new_values)
[7, 5, 2, 1]
>>> print(new_items)
['town', 'apple', 'car', 'phone']
>>>

cheers!

tiefeng wu
2009-04-22
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090422/b2783cfc/attachment.html>


More information about the Python-list mailing list