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


Groups > comp.lang.python > #35486

Re: Custom alphabetical sort

Path csiph.com!usenet.pasdenom.info!news.albasani.net!nntp-feed.chiark.greenend.org.uk!ewrotcd!news.nosignal.org!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <d@davea.name>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.013
X-Spam-Evidence '*H*': 0.97; '*S*': 0.00; 'exception': 0.03; 'table.': 0.07; 'missed': 0.09; 'python': 0.09; 'def': 0.10; 'int.': 0.16; 'least,': 0.16; 'wrote:': 0.17; 'comparing': 0.17; 'pfxlen:0': 0.17; "shouldn't": 0.17; 'code.': 0.20; 'keyerror:': 0.22; "i'd": 0.22; '15,': 0.23; 'to:2**1': 0.23; 'header:In-Reply- To:1': 0.25; 'header:User-Agent:1': 0.26; 'skip:[ 10': 0.26; '(most': 0.27; 'prevent': 0.27; 'character': 0.29; "i'm": 0.29; 'file': 0.32; 'to:no real name:2**1': 0.32; 'traceback': 0.33; 'to:addr:python-list': 0.33; 'thanks': 0.34; 'pm,': 0.35; 'skip:t 40': 0.37; 'subject:: ': 0.38; 'sure': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'received:192.168': 0.40; 'thomas': 0.62; 'more': 0.63; 'header:Reply-To:1': 0.68; 'received:74.208': 0.71; 'reply-to:no real name:2**0': 0.72; 'compact,': 0.84; 'approach.': 0.91
Date Tue, 25 Dec 2012 01:18:37 -0500
From Dave Angel <d@davea.name>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121011 Thunderbird/16.0.1
MIME-Version 1.0
To python-list@python.org, pander.musubi@gmail.com
Subject Re: Custom alphabetical sort
References <roy-BEEA73.11183724122012@news.panix.com> <mailman.1262.1356372812.29569.python-list@python.org> <945054f8-9827-4aaf-9c4d-00970404ca18@googlegroups.com>
In-Reply-To <945054f8-9827-4aaf-9c4d-00970404ca18@googlegroups.com>
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding 7bit
X-Provags-ID V02:K0:NDSDGVELt8JjveLRQHfFWEKHdlR078A7HrPJNIpJw05 AMxAPWkE43hX9+0HOJkdB54rJ/BYCgI+jrWaw97y7w92z/U+ez ixhYV3YkTLXO1dH3ZdFWwtcE6o0I3M5huxms4zTXIps/tfk8EX 3yDvk40V01H2MWoqRkjRMQrEcYguDLnqL3kzziDPiAvLl/hpM+ bpWpLzPWCJSox1kiof6NwcWFAEd/cGsOQrkgJFVSwZNDLAOka0 jeGXhjLmZR1jmYf0Qp72rfi0dUpM1cMtoWRPa2Ev1U7GnJ9McX vz3JN2rL6pr6KBhoN0hiRdREF/gwAcsmeDSno+JaVjXK1AqU5t 7Vq677RVyJlffVV/Aqvo=
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
Reply-To d@davea.name
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.1268.1356416441.29569.python-list@python.org> (permalink)
Lines 34
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1356416441 news.xs4all.nl 6880 [2001:888:2000:d::a6]:39604
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:35486

Show key headers only | View raw


On 12/24/2012 06:19 PM, Pander Musubi wrote:
> <snip>

> to prevent
>
> Traceback (most recent call last):
>   File "./sort.py", line 23, in <module>
>     things_to_sort.sort(key=string2sortlist)
>   File "./sort.py", line 15, in string2sortlist
>     return [hashindex[s] for s in string]
> KeyError: '\xc3'
>
> Thanks very much for this efficient code.

Perhaps you missed Ian Kelly's correction of Thomas Bach's approach:

d = { k: v for v, k in enumerate(cs) }


def collate(x):
    return list(map(d.get, x))

sorted(data, key=collate)

I'd use Ian Kelly's approach.  It's not only more compact, it shouldn't
give an exception for a character not in the table.  At least, not for
Python 2.x.  I'm not sure about Python 3, since it can give an exception
comparing None to int.


-- 

DaveA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Re: Custom alphabetical sort Roy Smith <roy@panix.com> - 2012-12-24 11:18 -0500
  Re: Custom alphabetical sort Pander Musubi <pander.musubi@gmail.com> - 2012-12-24 08:40 -0800
    Re: Custom alphabetical sort Roy Smith <roy@panix.com> - 2012-12-24 12:40 -0500
      Re: Custom alphabetical sort Pander Musubi <pander.musubi@gmail.com> - 2012-12-24 09:53 -0800
      Re: Custom alphabetical sort Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-12-24 18:07 +0000
  Re: Custom alphabetical sort Joshua Landau <joshua.landau.ws@gmail.com> - 2012-12-24 18:12 +0000
    Re: Custom alphabetical sort Pander Musubi <pander.musubi@gmail.com> - 2012-12-24 15:19 -0800
      Re: Custom alphabetical sort Dave Angel <d@davea.name> - 2012-12-25 01:18 -0500
      Re: Custom alphabetical sort Joshua Landau <joshua.landau.ws@gmail.com> - 2012-12-27 01:13 +0000
    Re: Custom alphabetical sort Pander Musubi <pander.musubi@gmail.com> - 2012-12-24 15:19 -0800
  Re: Custom alphabetical sort Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-12-24 22:57 +0000

csiph-web