Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #32550
| Newsgroups | comp.lang.python |
|---|---|
| Date | 2012-11-01 01:52 -0700 |
| References | <k6rfdu$rgn$1@news.albasani.net> |
| Message-ID | <9296aee8-23b4-4376-a77f-7a032d1e5d23@googlegroups.com> (permalink) |
| Subject | Re: sort order for strings of digits |
| From | wxjmfauth@gmail.com |
Le mercredi 31 octobre 2012 16:17:19 UTC+1, djc a écrit :
> I learn lots of useful things from the list, some not always welcome. No
>
> sooner had I found a solution to a minor inconvenience in my code, than
>
> a recent thread here drew my attention to the fact that it will not work
>
> for python 3. So suggestions please:
>
>
>
> TODO 2012-10-22: sort order numbers first then alphanumeric
>
> >>> n
>
> ('1', '10', '101', '3', '40', '31', '13', '2', '2000')
>
> >>> s
>
> ('a', 'ab', 'acd', 'bcd', '1a', 'a1', '222 bb', 'b a 4')
>
>
>
> >>> sorted(n)
>
> ['1', '10', '101', '13', '2', '2000', '3', '31', '40']
>
> >>> sorted(s)
>
> ['1a', '222 bb', 'a', 'a1', 'ab', 'acd', 'b a 4', 'bcd']
>
> >>> sorted(n+s)
>
> ['1', '10', '101', '13', '1a', '2', '2000', '222 bb', '3', '31', '40',
>
> 'a', 'a1', 'ab', 'acd', 'b a 4', 'bcd']
>
>
>
>
>
>
>
> Possibly there is a better way but for Python 2.7 this gives the
>
> required result
>
>
>
> Python 2.7.3 (default, Sep 26 2012, 21:51:14)
>
>
>
> >>> sorted(int(x) if x.isdigit() else x for x in n+s)
>
> [1, 2, 3, 10, 13, 31, 40, 101, 2000, '1a', '222 bb', 'a', 'a1', 'ab',
>
> 'acd', 'b a 4', 'bcd']
>
>
>
>
>
> [str(x) for x in sorted(int(x) if x.isdigit() else x for x in n+s)]
>
> ['1', '2', '3', '10', '13', '31', '40', '101', '2000', '1a', '222 bb',
>
> 'a', 'a1', 'ab', 'acd', 'b a 4', 'bcd']
>
>
>
>
>
> But not for Python 3
>
> Python 3.2.3 (default, Oct 19 2012, 19:53:16)
>
>
>
> >>> sorted(n+s)
>
> ['1', '10', '101', '13', '1a', '2', '2000', '222 bb', '3', '31', '40',
>
> 'a', 'a1', 'ab', 'acd', 'b a 4', 'bcd']
>
>
>
> >>> sorted(int(x) if x.isdigit() else x for x in n+s)
>
> Traceback (most recent call last):
>
> File "<stdin>", line 1, in <module>
>
> TypeError: unorderable types: str() < int()
>
> >>>
>
>
>
> The best I can think of is to split the input sequence into two lists,
>
> sort each and then join them.
>
>
>
>
>
> --
>
> djc
>>> # Py 3.2.3
>>> z = ['1', '10', '101', '13', '1a', '2', '2000',
... '222 bb', '3', '31', '40', 'a', 'a1', 'ab',
... 'acd', 'b a 4', 'bcd'
... ]
>>> n, s = [], []
>>> for e in z:
... if e.isdigit():
... n.append(int(e))
... else:
... s.append(e)
...
>>> n.sort()
>>> s.sort()
>>> ns = [str(e) for e in n]
>>> ns.extend(s)
>>> ns
['1', '2', '3', '10', '13', '31', '40', '101', '2000', '1a',
'222 bb', 'a', 'a1', 'ab', 'acd', 'b a 4', 'bcd']
jmf
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
sort order for strings of digits djc <djc@kangoo.invalid> - 2012-10-31 15:17 +0000
Re: sort order for strings of digits Hans Mulder <hansmu@xs4all.nl> - 2012-10-31 16:31 +0100
Re: sort order for strings of digits Ian Kelly <ian.g.kelly@gmail.com> - 2012-10-31 09:44 -0600
Re: sort order for strings of digits Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-10-31 14:17 -0400
Re: sort order for strings of digits Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-10-31 21:33 +0000
Re: sort order for strings of digits Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-10-31 19:05 -0400
Re: sort order for strings of digits Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-31 23:44 +0000
Re: sort order for strings of digits Chris Angelico <rosuav@gmail.com> - 2012-11-01 11:53 +1100
Re: sort order for strings of digits Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-11-02 00:27 +0000
Re: sort order for strings of digits Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-31 23:09 +0000
Re: sort order for strings of digits DJC <djc@news.invalid> - 2012-10-31 23:45 +0000
Re: sort order for strings of digits Arnaud Delobelle <arnodel@gmail.com> - 2012-11-01 00:59 +0000
Re: sort order for strings of digits Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-11-01 00:30 +0000
Re: sort order for strings of digits wxjmfauth@gmail.com - 2012-11-01 01:52 -0700
csiph-web