Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #32548
| References | <k6rfdu$rgn$1@news.albasani.net> <5091afc0$0$29967$c3e8da3$5496439d@news.astraweb.com> |
|---|---|
| Date | 2012-11-01 00:59 +0000 |
| Subject | Re: sort order for strings of digits |
| From | Arnaud Delobelle <arnodel@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3135.1351731559.27098.python-list@python.org> (permalink) |
On 31 October 2012 23:09, Steven D'Aprano <steve+comp.lang.python@pearwood.info> wrote: > The trick is to take each string and split it into a leading number and a > trailing alphanumeric string. Either part may be "empty". Here's a pure > Python solution: > > from sys import maxsize # use maxint in Python 2 > def split(s): > for i, c in enumerate(s): > if not c.isdigit(): > break > else: # aligned with the FOR, not the IF > return (int(s), '') > return (int(s[:i] or maxsize), s[i:]) > > Now sort using this as a key function: > > py> L = ['9', '1000', 'abc2', '55', '1', 'abc', '55a', '1a'] > py> sorted(L, key=split) > ['1', '1a', '9', '55', '55a', '1000', 'abc', 'abc2'] You don't actually need to split the string, it's enough to return a pair consisting of the number of leading digits followed by the string as the key. Here's an implementation using takewhile: >>> from itertools import takewhile >>> def prefix(s): ... return sum(1 for c in takewhile(str.isdigit, s)) or 1000, s ... >>> L = ['9', '1000', 'abc2', '55', '1', 'abc', '55a', '1a'] >>> sorted(L, key=prefix) ['1', '1a', '9', '55', '55a', '1000', 'abc', 'abc2'] Here's why it works: >>> map(prefix, L) [(1, '9'), (4, '1000'), (1000, 'abc2'), (2, '55'), (1, '1'), (1000, 'abc'), (2, '55a'), (1, '1a')] -- Arnaud
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
sort order for strings of digits djc <djc@kangoo.invalid> - 2012-10-31 15:17 +0000
Re: sort order for strings of digits Hans Mulder <hansmu@xs4all.nl> - 2012-10-31 16:31 +0100
Re: sort order for strings of digits Ian Kelly <ian.g.kelly@gmail.com> - 2012-10-31 09:44 -0600
Re: sort order for strings of digits Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-10-31 14:17 -0400
Re: sort order for strings of digits Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-10-31 21:33 +0000
Re: sort order for strings of digits Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-10-31 19:05 -0400
Re: sort order for strings of digits Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-31 23:44 +0000
Re: sort order for strings of digits Chris Angelico <rosuav@gmail.com> - 2012-11-01 11:53 +1100
Re: sort order for strings of digits Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-11-02 00:27 +0000
Re: sort order for strings of digits Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-31 23:09 +0000
Re: sort order for strings of digits DJC <djc@news.invalid> - 2012-10-31 23:45 +0000
Re: sort order for strings of digits Arnaud Delobelle <arnodel@gmail.com> - 2012-11-01 00:59 +0000
Re: sort order for strings of digits Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-11-01 00:30 +0000
Re: sort order for strings of digits wxjmfauth@gmail.com - 2012-11-01 01:52 -0700
csiph-web