[Tutor] samples on sort method of sequence object.

Hugo Arts hugo.yoshi at gmail.com
Wed Jan 13 15:25:06 CET 2010


I should note that this doesn't solve the general case of mixed
numeric/alphabetic sorting:

>>> a = ['a_1', 'a_2', 'a_3', 'a_4', 'a_10', 'b_1', 'b_2', 'b_3', 'b_4',
'b_10']
>>> a.sort(key=lambda x: int(x.split('_')[-1]))
>>> a
['a_1', 'b_1', 'a_2', 'b_2', 'a_3', 'b_3', 'a_4', 'b_4', 'a_10', 'b_10']

It only works if the alphabetic part of the string is the same everywhere,
and it doesn't work with a collection where some numbers appear before, some
after, etc.

Here is my solution for the general case:

from itertools import groupby
def alphanum_key(string):
    t = []
    for isdigit, group in groupby(string, str.isdigit):
        group = ''.join(group)
        t.append(int(group) if isdigit else group)
    return t

or, in a oneliner:

>>> alphanum_key = lambda x: tuple((int(''.join(g)) if k else ''.join(g))
for k, g in groupby(x, str.isdigit))

that's equivalent, but not very readable. It works by grouping sequences of
numbers and not numbers together, and treating the number sequences as
integers rather than strings.

>>> a = ['0a', '1a', '10a', 'a1', 'a2', 'a10', 'a11', 'b2', 'b3', 'b20']
>>> a.sort()
>>> a
['0a', '10a', '1a', 'a1', 'a10', 'a11', 'a2', 'b2', 'b20', 'b3']
>>> # regular sort messes up because 'a10' is alphabetically before 'a2'.
now with alphanumeric sort:
>>> a.sort(key=alphanum_key)
>>> a
['0a', '1a', '10a', 'a1', 'a2', 'a10', 'a11', 'b2', 'b3', 'b20']

Hugo

On Wed, Jan 13, 2010 at 2:32 PM, Albert-Jan Roskam <fomcl at yahoo.com> wrote:

>  Very concise == neat solutions. Yummy. ;-) Thanks for your replies!
>
>
> Cheers!!
> Albert-Jan
>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> In the face of ambiguity, refuse the temptation to guess.
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> --- On *Wed, 1/13/10, Sander Sweers <sander.sweers at gmail.com>* wrote:
>
>
> From: Sander Sweers <sander.sweers at gmail.com>
>
> Subject: Re: [Tutor] samples on sort method of sequence object.
> To: "Albert-Jan Roskam" <fomcl at yahoo.com>
> Cc: "*tutor python" <tutor at python.org>
> Date: Wednesday, January 13, 2010, 2:14 PM
>
>
> 2010/1/13 Albert-Jan Roskam <fomcl at yahoo.com<http://us.mc1107.mail.yahoo.com/mc/compose?to=fomcl@yahoo.com>
> >
> >
> > Interesting. Can this also be used to make sorting of alphanumerical list
> items in a 'numerical' way easier?
> > >>> 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']
>
> Yes.
> >>> 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']
> >>> sorted(x, key=lamda x: int(x.strip('var_')))
> SyntaxError: invalid syntax
> >>> sorted(x, key=lambda x: int(x.strip('var_')))
> ['var_0', 'var_1', 'var_2', 'var_3', 'var_4', 'var_5', 'var_6',
> 'var_7', 'var_8', 'var_9', 'var_10', 'var_11', 'var_12', 'var_13',
> 'var_14']
>
> Greets
> Sander
>
>
>
> _______________________________________________
> 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/b6c26003/attachment-0001.htm>


More information about the Tutor mailing list