partial list sort
Manuel M. Garcia
mgarcia at cole-switches.com
Thu Oct 10 13:56:33 EDT 2002
On Thu, 10 Oct 2002 10:10:44 GMT, Alex Martelli <aleax at aleax.it>
wrote:
>Here's my suggestion (untested):
>def jsaulsort(alist):
> aux = [ (i%3, alist[i][1], alist[i]) for i in range(len(alist)) ]
> aux.sort()
> alist[:] = [ item[2] for item in aux ]
This is the best one yet! One little bug, you meant to use the
integer divide "//" instead of modulo "%".
Python code should strive for clarity and consistency. And the
Decorate - Sort - Undecorate (DSU) technique is clear and applicable
over a wide range of custom sorts.
def jsaulsort(alist):
aux = [ (i//3, alist[i][1], alist[i]) for i in range(len(alist)) ]
aux.sort()
alist[:] = [ item[2] for item in aux ]
somelist=[ [1,'C'], [2,'B'], [3,'A'],
[4,'C'], [5,'B'], [6,'A'],
[-13,'B'], [7,'C'], [19,'A'] ]
print 'before: %s' % (somelist)
jsaulsort(somelist)
print 'after: %s' % (somelist)
before: [[1, 'C'], [2, 'B'], [3, 'A'], [4, 'C'], [5, 'B'], [6, 'A'],
[-13, 'B'], [7, 'C'], [19, 'A']]
after: [[3, 'A'], [2, 'B'], [1, 'C'], [6, 'A'], [5, 'B'], [4, 'C'],
[19, 'A'], [-13, 'B'], [7, 'C']]
More information about the Python-list
mailing list