Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Sorting a list with entries of unequal types

Reply
Thread Tools

Re: Sorting a list with entries of unequal types

 
 
Peter Otten
Guest
Posts: n/a
 
      01-29-2010
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
 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      01-29-2010
On Fri, 29 Jan 2010 12:13:50 +1100, Ben Finney wrote:

> Paul Rubin <(E-Mail Removed)> writes:
>
>> Ben Finney <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Terry Reedy
Guest
Posts: n/a
 
      01-29-2010
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.


 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      01-29-2010
Ben Finney wrote:

> Peter Otten <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Picking X random entries from linked list of Y entries Don Bruder C Programming 3 08-03-2010 09:10 AM
Unequal Load Balancing + GLBP Lior Shadhan Cisco 0 01-05-2005 07:20 AM
Tying up Port Login table entries with Port Table Entries in CISCO SNMP John Ramsden Cisco 0 07-24-2004 04:03 PM
BGP load sharing unequal paths - how would you do it? Rob Cisco 7 02-21-2004 07:47 PM
Problem - OSPF Unequal Cost Load Balancing (Sort of) NNTP Reader Cisco 7 07-25-2003 12:52 AM



Advertisments