sorting two corresponding lists?

nathanielpeterson08 at gmail.com nathanielpeterson08 at gmail.com
Tue Apr 21 12:40:40 EDT 2009


>From http://wiki.python.org/moin/HowTo/Sorting#Sortingbykeys, using the Decorate-Sort-Undecorate (aka Schwartzian transform) idiom:

#!/usr/bin/env python
items = ['apple', 'car', 'town', 'phone']
values = [5, 2, 7, 1]
zipped=zip(values,items)
zipped.sort(reverse=True)
values_sorted,items_sorted=zip(*zipped)
print values_sorted
# (7, 5, 2, 1)
print items_sorted
# ('town', 'apple', 'car', 'phone')



More information about the Python-list mailing list