How do I sort these?

Steve Holden steve at holdenweb.com
Fri Oct 28 15:00:42 EDT 2005


KraftDiner wrote:
> I have two lists.
> I want to sort by a value in the first list and have the second list
> sorted as well... Any suggestions on how I should/could do this?
> 
 >>> first = [1, 3, 5, 7, 9, 2, 4, 6, 8]
 >>> second = ['one', 'three', 'five', 'seven', 'nine', 'two', 'four', 
'six', 'eight']
 >>> both = zip(first, second)
 >>> both.sort()
 >>> [b[0] for b in both]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
 >>> [b[1] for b in both]
['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
 >>>

You mean like this?

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC                     www.holdenweb.com
PyCon TX 2006                  www.python.org/pycon/




More information about the Python-list mailing list