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


Groups > comp.lang.python > #35463

Re: Custom alphabetical sort

Path csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <thbach@students.uni-mainz.de>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.007
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'parameter': 0.07; "'w',": 0.09; 'dec': 0.15; "skip:' 30": 0.15; "'0',": 0.16; "'a',": 0.16; "'b',": 0.16; "'c',": 0.16; "'d',": 0.16; "'e',": 0.16; "'o',": 0.16; "'r',": 0.16; "'z')": 0.16; '24,': 0.16; 'mon,': 0.16; 'wrote:': 0.17; '>>>': 0.18; 'appropriate': 0.20; 'sort': 0.21; 'import': 0.21; 'random': 0.24; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; "skip:' 20": 0.32; 'to:addr:python- list': 0.33; 'mapping': 0.35; 'option': 0.37; 'subject:: ': 0.38; 'received:10': 0.38; 'to:addr:python.org': 0.39; 'content- disposition:inline': 0.60; '8bit%:50': 0.65; "'2',": 0.84; "'3',": 0.84; 'order:': 0.84; 'received:10.94': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=uni-mainz.de; i=@uni-mainz.de; q=dns/txt; s=ironport; t=1356365474; x=1387901474; h=date:from:to:subject:message-id:references:mime-version: content-transfer-encoding:in-reply-to; bh=n1SHupLsb4Duq7pZ22igpEYuHvqYFZ64Mt59BTKvPpc=; b=OV/92A4KmTMiauxN5kaKHO+oY+eWOOP0dC+wyZirk0Ga1eMM6qjiBsjq dT95pgE+lUXMKEKCAt3Bl/Ebq7nuUDjbC9/FePlVnmJ385tzb/4A6H8KR 6kAwjxqbCJZk6BNQ2BG4DRtTvLCW074oPoNDY2cB7PweWR4PexJdSE+4/ U=;
X-IronPort-Anti-Spam-Filtered true
X-IronPort-Anti-Spam-Result ArwGANV92FAKXgZY/2dsb2JhbABEg0iCGLgcc4IeAQEEAQwmAVYJAhguFCkgiCAKiF2tDYxXgRqCSGEDiC2KLIMyAZBIgnWCIQ
Date Mon, 24 Dec 2012 17:11:03 +0100
From Thomas Bach <thbach@students.uni-mainz.de>
To <python-list@python.org>
Subject Re: Custom alphabetical sort
References <40d108ec-b019-4829-a969-c8ef513866f1@googlegroups.com>
MIME-Version 1.0
Content-Type text/plain; charset="iso-8859-1"
Content-Disposition inline
Content-Transfer-Encoding 8bit
In-Reply-To <40d108ec-b019-4829-a969-c8ef513866f1@googlegroups.com>
User-Agent Mutt/1.5.21 (2010-09-15)
X-Originating-IP [88.68.6.180]
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.1257.1356365475.29569.python-list@python.org> (permalink)
Lines 20
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1356365475 news.xs4all.nl 6982 [2001:888:2000:d::a6]:55035
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:35463

Show key headers only | View raw


On Mon, Dec 24, 2012 at 07:32:56AM -0800, Pander Musubi wrote:
> I would like to sort according to this order:
> 
> (' ', '.', '\'', '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'A', 'ä', 'Ä', 'á', 'Á', 'â', 'Â', 'à', 'À', 'å', 'Å', 'b', 'B', 'c', 'C', 'ç', 'Ç', 'd', 'D', 'e', 'E', 'ë', 'Ë', 'é', 'É', 'ê', 'Ê', 'è', 'È', 'f', 'F', 'g', 'G', 'h', 'H', 'i', 'I', 'ï', 'Ï', 'í', 'Í', 'î', 'Î', 'ì', 'Ì', 'j', 'J', 'k', 'K', 'l', 'L', 'm', 'M', 'n', 'ñ', 'N', 'Ñ', 'o', 'O', 'ö', 'Ö', 'ó', 'Ó', 'ô', 'Ô', 'ò', 'Ò', 'ø', 'Ø', 'p', 'P', 'q', 'Q', 'r', 'R', 's', 'S', 't', 'T', 'u', 'U', 'ü', 'Ü', 'ú', 'Ú', 'û', 'Û', 'ù', 'Ù', 'v', 'V', 'w', 'W', 'x', 'X', 'y', 'Y', 'z', 'Z')
> 

One option is to use sorted's key parameter with an appropriate
mapping in a dictionary:

>>> cs = (' ', '.', '\'', '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'A', 'ä', 'Ä', 'á', 'Á', 'â', 'Â', 'à', 'À', 'å', 'Å', 'b', 'B', 'c', 'C', 'ç', 'Ç', 'd', 'D', 'e', 'E', 'ë', 'Ë', 'é', 'É', 'ê', 'Ê', 'è', 'È', 'f', 'F', 'g', 'G', 'h', 'H', 'i', 'I', 'ï', 'Ï', 'í', 'Í', 'î', 'Î', 'ì', 'Ì', 'j', 'J', 'k', 'K', 'l', 'L', 'm', 'M', 'n', 'ñ', 'N', 'Ñ', 'o', 'O', 'ö', 'Ö', 'ó', 'Ó', 'ô', 'Ô', 'ò', 'Ò', 'ø', 'Ø', 'p', 'P', 'q', 'Q', 'r', 'R', 's', 'S', 't', 'T', 'u', 'U', 'ü', 'Ü', 'ú', 'Ú', 'û', 'Û', 'ù', 'Ù', 'v', 'V', 'w', 'W', 'x', 'X', 'y', 'Y', 'z', 'Z')

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

>>> import random

>>> ''.join(sorted(random.sample(cs, 20), key=d.get))
'5aAàÀåBCçËÉíÎLÖøquùx'

Regards,
	Thomas.

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


Thread

Custom alphabetical sort Pander Musubi <pander.musubi@gmail.com> - 2012-12-24 07:32 -0800
  Re: Custom alphabetical sort Thomas Bach <thbach@students.uni-mainz.de> - 2012-12-24 17:11 +0100
    Re: Custom alphabetical sort Pander Musubi <pander.musubi@gmail.com> - 2012-12-24 08:34 -0800
      Re: Custom alphabetical sort Ian Kelly <ian.g.kelly@gmail.com> - 2012-12-24 10:29 -0700
    Re: Custom alphabetical sort Pander Musubi <pander.musubi@gmail.com> - 2012-12-24 08:34 -0800
  Re: Custom alphabetical sort wxjmfauth@gmail.com - 2012-12-27 10:17 -0800
    Re: Custom alphabetical sort Terry Reedy <tjreedy@udel.edu> - 2012-12-27 17:17 -0500
    Re: Custom alphabetical sort Ian Kelly <ian.g.kelly@gmail.com> - 2012-12-27 16:17 -0700
      Re: Custom alphabetical sort wxjmfauth@gmail.com - 2012-12-28 02:27 -0800

csiph-web