473,385 Members | 1,387 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 1939
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.