Python 3.0 - is this true?
Arnaud Delobelle
arnodel at googlemail.com
Sun Nov 9 03:19:26 EST 2008
Kay Schluehr <kay.schluehr at gmx.net> writes:
> On 9 Nov., 07:06, Steven D'Aprano <st... at REMOVE-THIS-
[...]
>> In any case, the above doesn't work now, since either L1 or L2 might
>> contain complex numbers.
>> The sorted() trick only works because you're
>> making an assumption about the kinds of things in the lists. If you want
>> to be completely general, the above solution isn't guaranteed to work.
>
> You are right. I never used complex numbers in Python so problems were
> not visible. Otherwise the following comp function in Python 2.X does
> the job:
>
> def comp(x1, x2):
> try:
> if x1<x2:
> return -1
> else:
> return 1
> except TypeError:
> if str(x1)<str(x2):
> return -1
> else:
> return 1
>
Sadly it fails on transitivity:
>>> comp(2, 3j)
-1
>>> comp(3j, True)
-1
>>> comp(True, 2)
-1
--
Arnaud
More information about the Python-list
mailing list