[Tutor] samples on sort method of sequence object.

Albert-Jan Roskam fomcl at yahoo.com
Wed Jan 13 13:51:30 CET 2010


Interesting. Can this also be used to make sorting of alphanumerical list items in a 'numerical' way easier? E.g.:
>>> x
['var_0', 'var_13', 'var_11', 'var_9', 'var_4', 'var_1', 'var_5', 'var_6', 'var_7', 'var_14', 'var_2', 'var_3', 'var_8', 'var_10', 'var_12']
>>> x.sort()
>>> x
['var_0', 'var_1', 'var_10', 'var_11', 'var_12', 'var_13', 'var_14', 'var_2', 'var_3', 'var_4', 'var_5', 'var_6', 'var_7', 'var_8', 'var_9']
>>> 
 
This clearly does not give the desired result. I once solved this by making a dictionary of which the keys are zfilled versions of the list items (e.g. var_01, etc), then sort the keys, get the dictionary values in the right order, etc. It seemed a cumbersome way. Isn't there an easier way to sort x?

Cheers!!
Albert-Jan

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In the face of ambiguity, refuse the temptation to guess.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--- On Tue, 1/12/10, Kent Johnson <kent37 at tds.net> wrote:


From: Kent Johnson <kent37 at tds.net>
Subject: Re: [Tutor] samples on sort method of sequence object.
To: "Make Twilight" <ph4nut at gmail.com>
Cc: tutor at python.org
Date: Tuesday, January 12, 2010, 7:53 PM


On Tue, Jan 12, 2010 at 9:39 AM, Make Twilight <ph4nut at gmail.com> wrote:

>  I can understand how to use parameters of cmp and reverse,except the
> key parameter...
>  Would anyone give me an example of using sort method with key parameter?

http://personalpages.tds.net/~kent37/kk/00007.html
Kent
_______________________________________________
Tutor maillist  -  Tutor at python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor



      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100113/8a692402/attachment.htm>


More information about the Tutor mailing list