472,791 Members | 1,600 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,791 software developers and data experts.

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 1907
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Brad Hagen | last post by:
I am trying to sort a vector of pointers to Foo, and my code looks something like this: vector<Foo*> *pNewVector sort(pNewVector->begin(), pNewVector->end(), Foo::SortFunction );
3
by: Tommo | last post by:
Hello All, whilst I know how to sort an array I am having problems with my situation, what I would ideally like is something like a 'sort keys' action with Perl hashes. I am reading in data and...
4
by: Peter A. Schott | last post by:
Trying to operate on a list of files similar to this: test.1 test.2 test.3 test.4 test.10 test.15 test.20
10
by: David Ricker | last post by:
I am having problems adding two numbers. I am trying to add 1.005 and 1.007 to come up with 2.012. Should be easy enough right? Problem is that I keep getting 2.0119999999999996 as my result. ...
5
by: JustSomeGuy | last post by:
According to the docs... http://www.cppreference.com/cpplist/sort.html There is a parameter to the std::list.sort method. I don't understand this methods use... I want to sort the list which...
3
by: dc24ua | last post by:
Hello - I need help sorting an xml file. I'd like to sort the xml based on the value found in <colvalue> of the second <col>. <?xml version="1.0" encoding="ISO-8859-1" ?> <?xml-stylesheet...
0
by: k1ckthem1dget | last post by:
I need to display the unsorted list of names and display the sorted list of names. My program is getting a bunch of errors though, and i dont know why. I am getting the following errors. 28:...
1
by: Studlyami | last post by:
Alright, I have a list which is constantly getting items added and items removed. I want the user to be able to click the header of the list control and sort the items by the column clicked. Ive...
0
by: Gordon Burditt | last post by:
>Given a text file as input, sort the lines and store in the specified If this is not homework, consider using the UNIX sort utility. There are ports of this program to Windows. The source code...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.