HELP:sorting list of outline numbers

F

Felix Collins

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
 
R

Robert Kern

Felix said:
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
(e-mail address removed)

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

Scott David Daniels

Felix said:
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
(e-mail address removed)
 
F

Felix Collins

Robert said:
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
 
F

Felix Collins

Felix said:
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
 
C

Christopher Subich

Felix said:
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]))]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top