On Tue, 22 Nov 2005 14:03:32 +0100, "Fredrik Lundh" <[EMAIL PROTECTED]> wrote:

>"Shi Mu" wrote:
>
>>I use Python 2.3 to run the following code:
>>>>> a=[[1,2],[4,8],[0,3]]
>>>>> a.sort()
>>>>> a
>> [[0, 3], [1, 2], [4, 8]]
>>>>>
>> I wonder whether the sort function automatically consider the first
>> element in the list of list as the sorting criteria or it just happens
>> to be?
>
>the documentation has the answer:
>
>    http://docs.python.org/ref/comparisons.html
>
>    "Tuples and lists are compared lexicographically using comparison of
>    corresponding elements. This means that to compare equal, each
>    element must compare equal and the two sequences must be of the
>    same type and have the same length.
>    If not equal, the sequences are ordered the same as their first differing
>    elements."
IMO it could be good to mention that this "first" means that the rest are then 
ignored,
and this fact means that you can safely "decorate" in DSU if you do it with 
unique
comparable values. I.e.,

 >>> (1, 1j) < (2, 1j)
 True
 >>> (1, 1j) < (1, 1j)
 False
 >>> (1, 1j) < (1, 2j)
 Traceback (most recent call last):
   File "<stdin>", line 1, in ?
 TypeError: cannot compare complex numbers using <, <=, >, >=

Interestingly, that "False" apparently means equality is detected before 
ordering is tested?

 >>> (1, 1j, 1) < (1, 1j, 2)
 True

IOW, element-pair < comparisons are evaluated until you get a True or reach the 
end,
and same_complex < same_complex can return a legititmate False if they're equal 
;-)

Or so I thought ;-/ What do you make of

 >>> 1j < 1j
 Traceback (most recent call last):
   File "<stdin>", line 1, in ?
 TypeError: cannot compare complex numbers using <, <=, >, >=
 >>> (1j,) < (1j,)
 False

Regards,
Bengt Richter
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to