Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
Python
sort order for strings of digits
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="djc, post: 5058396"] I learn lots of useful things from the list, some not always welcome. No sooner had I found a solution to a minor inconvenience in my code, than a recent thread here drew my attention to the fact that it will not work for python 3. So suggestions please: TODO 2012-10-22: sort order numbers first then alphanumeric('a', 'ab', 'acd', 'bcd', '1a', 'a1', '222 bb', 'b a 4') ['1', '10', '101', '13', '1a', '2', '2000', '222 bb', '3', '31', '40', 'a', 'a1', 'ab', 'acd', 'b a 4', 'bcd'] Possibly there is a better way but for Python 2.7 this gives the required result Python 2.7.3 (default, Sep 26 2012, 21:51:14) [1, 2, 3, 10, 13, 31, 40, 101, 2000, '1a', '222 bb', 'a', 'a1', 'ab', 'acd', 'b a 4', 'bcd'] [str(x) for x in sorted(int(x) if x.isdigit() else x for x in n+s)] ['1', '2', '3', '10', '13', '31', '40', '101', '2000', '1a', '222 bb', 'a', 'a1', 'ab', 'acd', 'b a 4', 'bcd'] But not for Python 3 Python 3.2.3 (default, Oct 19 2012, 19:53:16) ['1', '10', '101', '13', '1a', '2', '2000', '222 bb', '3', '31', '40', 'a', 'a1', 'ab', 'acd', 'b a 4', 'bcd'] Traceback (most recent call last): The best I can think of is to split the input sequence into two lists, sort each and then join them. [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Python
sort order for strings of digits
Top