469,612 Members | 2,568 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,612 developers. It's quick & easy.

HELP:sorting list of outline numbers

Hi All,
does anyone know any cleaver tricks to sort a list of outline numbers.

An outline number is a number of the form... 1.2.3

they should be sorted in the following way...

1
1.1
1.2
1.12

python's alpha sort (by design) sorts them...

1
1.1
1.12
1.2

That's no good for me.
I'm planning on splitting the strings into multiple lists of ints and
doing numerical sorts.

Thanks for any clever ideas that might make it easier.

Felix
Aug 3 '05 #1
5 1778
Felix Collins wrote:
Hi All,
does anyone know any cleaver tricks to sort a list of outline numbers.

For 2.4 and beyond: (using a key function)

def numparts(outlinetext):
return [int(number) for number in outlinetext.split('.')]

lst = ['1', '1.2', '1.12', '1.1', '3.1']
lst.sort(key=numparts)

For 2.3: (using DSU -- Decorate, Sort, Undecorate)
def numparts(outlinetext):
return [int(number) for number in outlinetext.split('.')]

lst = ['1', '1.2', '1.12', '1.1', '3.1']
decorated = [(numparts(txt), txt) for txt in lst]
decorated.sort()
lst[:] = [txt for code, txt in decorated]

--Scott David Daniels
Sc***********@Acm.Org
Aug 3 '05 #2
Felix Collins wrote:
Hi All,
does anyone know any cleaver tricks to sort a list of outline numbers.

An outline number is a number of the form... 1.2.3

they should be sorted in the following way...

1
1.1
1.2
1.12

python's alpha sort (by design) sorts them...

1
1.1
1.12
1.2

That's no good for me.
I'm planning on splitting the strings into multiple lists of ints and
doing numerical sorts.

Thanks for any clever ideas that might make it easier.


Use the "key" keyword argument to list.sort().

In [1]: outline = ['1.12', '1.1', '1', '1.2']

In [2]: outline.sort(key=lambda x: map(int, x.split('.')))

In [3]: outline
Out[3]: ['1', '1.1', '1.2', '1.12']

--
Robert Kern
rk***@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

Aug 3 '05 #3
Robert Kern wrote:
Felix Collins wrote:

Use the "key" keyword argument to list.sort().

In [1]: outline = ['1.12', '1.1', '1', '1.2']

In [2]: outline.sort(key=lambda x: map(int, x.split('.')))

In [3]: outline
Out[3]: ['1', '1.1', '1.2', '1.12']

Is this new in 2.4? I have to use 2.3 as I'm working with Trac.

Traceback (most recent call last):
File "<pyshell#21>", line 1, in -toplevel-
keys.sort(key=lambda x: map(int, x.split('.')))
TypeError: sort() takes no keyword arguments
Thanks Scott and Robert for your quick help. This list is amazing!

Regards,
Felix
Aug 3 '05 #4
Felix Collins wrote:

Thanks Scott and Robert for your quick help. This list is amazing!

Regards,
Felix


Using Decorate, Sort , Undecorate...

works like a charm.

Thanks again.

Felix
Aug 3 '05 #5
Felix Collins wrote:
Using Decorate, Sort , Undecorate...

works like a charm.


As a one-liner, you can also deconstruct and rebuild the outline numbers:

new_outline = ['.'.join(v) for v in (sorted([k.split('.') for k in
old_outline]))]
Aug 3 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Brad Hagen | last post: by
4 posts views Thread by Peter A. Schott | last post: by
10 posts views Thread by David Ricker | last post: by
5 posts views Thread by JustSomeGuy | last post: by
3 posts views Thread by dc24ua | last post: by
1 post views Thread by Studlyami | last post: by
reply views Thread by Gordon Burditt | last post: by
reply views Thread by devrayhaan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.