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: 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 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: <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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 On 12/24/2012 06:19 PM, Pander Musubi wrote: > > to prevent > > Traceback (most recent call last): > File "./sort.py", line 23, in > 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