'1' + 1 ==> True ???

Dan Bishop danb_83 at yahoo.com
Thu Mar 18 15:59:26 EST 2004


Peter Hansen <peter at engcorp.com> wrote in message news:<pdKdnSicPq2WNcTdRVn-sw at powergate.ca>...
> Nicola Mingotti wrote:
> > This is all what i wanted to know . 
> > Even if i can't see it's utility, at least
> > for the behaviour of the comparison between strings
> > and numbers with every string considered greater of every int .
> 
> One example of its utility: in a sort algorithm, you would want to be 
> able to produce consistent orderings of items in a list, even if some of 
> the items were strings, and others were integers.  (That may in fact be 
> its _only_ utility, for all I know.)

Then, instead of having broken operators in the language, why not
redefine the sort routine?

>>> def _transform(x):
...    if isinstance(x, (int, long, float)):
...       return ('', x)
...    elif isinstance(x, complex):
...       return ('', x.real, x.imag)
...    else:
...       return (type(x).__name__, x)
...
>>> def _untransform(tpl):
...    if len(tpl) == 3:
...       return complex(tpl[1], tpl[2])
...    else:
...       return tpl[1]
...
>>> def sortedCopy(lst):
...    transformedList = [_transform(x) for x in lst]
...    transformedList.sort()
...    return [_untransform(x) for x in transformedList]
...
>>> sortedCopy([2L, 'spam', 42, 2+1j, 3.14159])
[2L, (2+1j), 3.1415899999999999, 42, 'spam']



More information about the Python-list mailing list