Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #32548

Re: sort order for strings of digits

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <arnodel@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'string.': 0.04; 'sys': 0.05; "'')": 0.07; 'key.': 0.07; 'trailing': 0.07; 'python': 0.09; '(1,': 0.09; 'function:': 0.09; 'received:mail- vc0-f174.google.com': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; '(2,': 0.16; 'for,': 0.16; 'itertools': 0.16; 's))': 0.16; 'to:addr:pearwood.info': 0.16; 'to:addr:steve+comp.lang.python': 0.16; "to:name:steven d'aprano": 0.16; 'string': 0.17; 'wrote:': 0.17; 'string,': 0.17; '>>>': 0.18; 'followed': 0.20; 'sort': 0.21; 'import': 0.21; 'cc:2**0': 0.23; 'split': 0.23; 'cc:no real name:2**0': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply- To:1': 0.25; 'message-id:@mail.gmail.com': 0.27; 'aligned': 0.29; 'consisting': 0.29; "d'aprano": 0.29; 'received:209.85.220.174': 0.29; 'steven': 0.29; 'received:google.com': 0.34; 'received:209.85.220': 0.35; 'received:209.85': 0.35; 'enough': 0.36; 'october': 0.37; 'why': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'header:Received:5': 0.40; 'leading': 0.61; 'alphanumeric': 0.65
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=+AyKDa8zvnkIJYnVx54KNegEP4uL06RuAmHd/NTjRGk=; b=iEL0T/37flAH/iD1MIZwqubDQcydYqd3fFinkKI12wItYZ8irMRpP/m+ieO0nQZ0Yq qekL6NECsH73qnz6r4qVX11MG4OVJIUjFeXr+bH1xx/e6hrETQLDvmZpXsJV4Emz+sRt +rTdVmk7LFs992EpFDckCmzCYX13a61NMLLDlv6nf2ifiE2MB3Yrl+GDBq3g7qFN9pFB KI81wJRILlVZXYM9VtzaJK2hqxCifLM/+O7VDpVxkww6lCMha3ImElOn7zJF8EqISir5 gpi0EN/Tf0fCA2Y8pr49CIKE7gVxvB6EtOlgYNytq6eBuXAQ3LA/cyPDNi3Y3LCi9mRm WeYg==
MIME-Version 1.0
In-Reply-To <5091afc0$0$29967$c3e8da3$5496439d@news.astraweb.com>
References <k6rfdu$rgn$1@news.albasani.net> <5091afc0$0$29967$c3e8da3$5496439d@news.astraweb.com>
Date Thu, 1 Nov 2012 00:59:10 +0000
Subject Re: sort order for strings of digits
From Arnaud Delobelle <arnodel@gmail.com>
To "Steven D'Aprano" <steve+comp.lang.python@pearwood.info>
Content-Type text/plain; charset=UTF-8
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3135.1351731559.27098.python-list@python.org> (permalink)
Lines 41
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1351731559 news.xs4all.nl 6960 [2001:888:2000:d::a6]:34054
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:32548

Show key headers only | View raw


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 | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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