Need a strange sort method...

SpreadTooThin bjobrien62 at gmail.com
Mon Oct 16 16:01:53 EDT 2006


SpreadTooThin wrote:
> Simon Brunning wrote:
> > On 10/16/06, Simon Brunning <simon at brunningonline.net> wrote:
> > > >>> a = [1,2,3,4,5,6,7,8,9,10]
> > > >>> a.sort(key=lambda item: (((item-1) %3), item))
> > > >>> a
> > > [1, 4, 7, 10, 2, 5, 8, 3, 6, 9]
> >
> > Re-reading the OP's post, perhaps sorting isn't what's required:
> >
> > >>> a[::3] + a[1::3] + a[2::3]
> > [1, 4, 7, 10, 2, 5, 8, 3, 6, 9]
> >
> > --
> > Cheers,
> > Simon B
> > simon at brunningonline.net
> > http://www.brunningonline.net/simon/blog/
>

> Ok so this is what I got.. but there is an odd side effect:
>
> def reslice(series):
> 	series.sort()
> 	i = 1
> 	newseries = series[::3]
> 	while(1):
> 		c = series[i::3]
> 		if len(c) >= 3:
> 			newseries = newseries + c
> 		else:
> 			break
> 		i = i + 1
> 	return newseries
>
> a = [2,1,4,3,6,5,8,7,10,9]
> b = reslice(a)
> print b
> >>> [1, 4, 7, 10, 2, 5, 8, 3, 6, 9, 4, 7, 10]
>
> I have one extra 10 that I shouldn't...


I think my loop termination is incorrect...
maybe I should just stop when my new series size is the same size as
the original?




More information about the Python-list mailing list