Re: Sorting a list with entries of unequal types

Discussion in 'Python' started by Peter Otten, Jan 29, 2010.

  1. Peter Otten

    Peter Otten Guest

    Ben Finney wrote:

    > Python 2.6 tells me that, in Python 3, sorting a list with entries of
    > incompatible types is not allowed:


    > $ python2.6 -3 -c "foo = [1, True, 'green', 4, -27, 15.3]; foo.sort();
    > print foo;" -c:1: DeprecationWarning: comparing unequal types not
    > supported in 3.x
    > [-27, 1, True, 4, 15.300000000000001, 'green']
    > =====
    >
    > So how should I be sorting a list with entries of “unequal types†such
    > that it will work in Python 3?


    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}
    >>> sorted([1, True, 'green', 4, -27, 15.3], key=key)

    [-27, 1, True, 4, 15.300000000000001, 'green']

    should work.

    Peter
    Peter Otten, Jan 29, 2010
    #1
    1. Advertising

  2. On Fri, 29 Jan 2010 12:13:50 +1100, Ben Finney wrote:

    > Paul Rubin <> writes:
    >
    >> Ben Finney <> 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
    Steven D'Aprano, Jan 29, 2010
    #2
    1. Advertising

  3. Peter Otten

    Terry Reedy Guest

    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.
    Terry Reedy, Jan 29, 2010
    #3
  4. Peter Otten

    Peter Otten Guest

    Ben Finney wrote:

    > Peter Otten <> 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.


    I don't see the limitation here. The above key functions sorts unknown types
    by name, then id(type) then instance.

    Peter
    Peter Otten, Jan 29, 2010
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Raistlin

    Comparing 2 unequal arrays...

    Raistlin, Sep 28, 2003, in forum: C++
    Replies:
    6
    Views:
    807
    Ron Natalie
    Sep 29, 2003
  2. Replies:
    1
    Views:
    553
    christian.bau
    Jan 15, 2007
  3. Unequal Length Maze

    , Aug 24, 2007, in forum: Python
    Replies:
    3
    Views:
    346
    Diez B. Roggisch
    Aug 24, 2007
  4. Don Bruder
    Replies:
    3
    Views:
    965
    spikeysnack
    Aug 3, 2010
  5. bolega
    Replies:
    1
    Views:
    681
    Stan Bischof
    Mar 28, 2011
Loading...

Share This Page