[Tutor] Sorting more than one list

py py at runsun.info
Fri Apr 1 03:16:18 CEST 2005


An alternative way of doing this (if you have python 2.4):

>>> ppl =   ['john', 'mary', 'lary', 'jane']
>>> age =   [15, 30, 23, 25]
>>> height= [160, 165, 178, 170]

>>> sortby = lambda a, b: [a[b.index(x)] for x in sorted(b)]
>>> sortby(ppl, age)
['john', 'lary', 'jane', 'mary']
>>> sortby(ppl, height)
['john', 'mary', 'jane', 'lary']

>>> age    # age is not changed
[15, 30, 23, 25]

 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20050401/64033487/attachment.html


More information about the Tutor mailing list