Prev: Great example of a python module/package following up to date conventions.
Next: Killing a Thread
From: Steven D'Aprano on 28 Jan 2010 20:11 On Fri, 29 Jan 2010 11:01:21 +1100, Ben Finney wrote: > So how should I be sorting a list with entries of “unequal types” such > that it will work in Python 3? That depends on how you want the items to be sorted. Python 2.x sorted unequal types in some arbitrary but consistent order. If that's all you want, then a key function like this will work in Python 3.1: >>> foo = [1, True, 'green', 4, -27, 15.3] >>> foo.sort() Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unorderable types: str() < bool() >>> foo.sort(key=lambda x: (str(type(x)), x)) >>> foo [True, 15.3, -27, 1, 4, 'green'] If you want to match the sort order from a specific version of Python, or if you want a specific ordering, you'll need to write your own key function. -- Steven
From: Ben Finney on 28 Jan 2010 20:13 Paul Rubin <no.email(a)nospam.invalid> writes: > Ben Finney <ben+python(a)benfinney.id.au> writes: > > So how should I be sorting a list with entries of “unequal types” > > such that it will work in Python 3? > > Um, what ordering do you want? The same ordering I'd get in Python 2; that is, determined by the types of the elements. Peter Otten <__peter__(a)web.de> writes: > I can't find the relevant part of the 2.6 documentation, but something like > > >>> def key(x): > ... t = type(x) > ... t = compat.get(t, t) > ... return t.__name__, id(t), x > ... > >>> compat = {bool: float, int: float} The problem with this approach is that it assumes I know all the types of elements that will be sorted; I don't. I want to sort a list coming from some other part of the code, and I don't want to arbitrarily limit the types of the list elements. Essentially, I want the same behaviour I'd get from Python 2's sort: the order of the elements is determined by the ordering built into the types of the elements themselves, with sensible defaults. -- \ “I distrust those people who know so well what God wants them | `\ to do to their fellows, because it always coincides with their | _o__) own desires.” —Susan Brownell Anthony, 1896 | Ben Finney
From: Steven D'Aprano on 28 Jan 2010 20:24 On Fri, 29 Jan 2010 12:13:50 +1100, Ben Finney wrote: > Paul Rubin <no.email(a)nospam.invalid> writes: > >> Ben Finney <ben+python(a)benfinney.id.au> writes: >> > So how should I be sorting a list with entries of “unequal types” >> > such that it will work in Python 3? >> >> Um, what ordering do you want? > > The same ordering I'd get in Python 2; that is, determined by the types > of the elements. The ordering has not been consistent across minor versions of Python 2: $ python2.0 Python 2.0.1 (#1, Jan 14 2010, 15:43:17) [GCC 4.1.2 20070925 (Red Hat 4.1.2-27)] on linux2 Type "copyright", "credits" or "license" for more information. >>> L = [None, {}, [], 1, "x"] >>> L.sort() >>> L [1, None, {}, [], 'x'] $ python2.6 Python 2.6.1 (r261:67515, Dec 24 2008, 00:33:13) [GCC 4.1.2 20070502 (Red Hat 4.1.2-12)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> L = [None, {}, [], 1, "x"] >>> L.sort() >>> L [None, 1, {}, [], 'x'] Python has never guaranteed a particular ordering for unequal types, only that it would be consistent between repeated calls to sort. -- Steven
From: Terry Reedy on 29 Jan 2010 01:18 On 1/28/2010 8:24 PM, Steven D'Aprano wrote: >>> Um, what ordering do you want? >> >> The same ordering I'd get in Python 2; that is, determined by the types >> of the elements. > > The ordering has not been consistent across minor versions of Python 2: And complex numbers, and probably some other things, cannot be sorted even in Python2. Universal sorting is a broken idea, so Python3 leaves it to you to say what *you* mean.
From: Ben Finney on 29 Jan 2010 02:08 Terry Reedy <tjreedy(a)udel.edu> writes: > And complex numbers, and probably some other things, cannot be sorted > even in Python2. Universal sorting is a broken idea, so Python3 leaves > it to you to say what *you* mean. Okay. I guess I'd better figure out what that is, for this code base, before porting it to Python 3 :-) Thanks for the helpful responses. -- \ “Most people, I think, don't even know what a rootkit is, so | `\ why should they care about it?” —Thomas Hesse, Sony BMG, 2006 | _o__) | Ben Finney
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 Prev: Great example of a python module/package following up to date conventions. Next: Killing a Thread |