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: 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: <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 To: "Steven D'Aprano" 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 On 31 October 2012 23:09, Steven D'Aprano 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