Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #2198
| Date | 2011-03-30 00:06 +0100 |
|---|---|
| From | MRAB <python@mrabarnett.plus.com> |
| Subject | Re: Guido rethinking removal of cmp from sort method |
| References | (11 earlier) <AANLkTinyEpTVaAjikP8M108ymj=J6LV2styp+Md-siwj@mail.gmail.com> <4D922B91.5030505@mrabarnett.plus.com> <AANLkTiktvGrpJM6AYP7yEYXj8heqz61ey3FuLSJG7wQH@mail.gmail.com> <AANLkTinfLaedDYKd9KeSihh5wz09fuwXN0jSV-jHQeAG@mail.gmail.com> <AANLkTinXCMTduz96su=avK4NN3PQLhgbzLi5jWA69W0g@mail.gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.1.1301440012.2990.python-list@python.org> (permalink) |
On 29/03/2011 21:32, Dan Stromberg wrote:
>
> On Tue, Mar 29, 2011 at 12:48 PM, Ian Kelly <ian.g.kelly@gmail.com
> <mailto:ian.g.kelly@gmail.com>> wrote:
>
> On Tue, Mar 29, 2011 at 1:08 PM, Chris Angelico <rosuav@gmail.com
> <mailto:rosuav@gmail.com>> wrote:
> > On Wed, Mar 30, 2011 at 5:57 AM, MRAB <python@mrabarnett.plus.com
> <mailto:python@mrabarnett.plus.com>> wrote:
> >> You would have to do more than that.
> >>
> >> For example, "" < "A", but if you "negate" both strings you get "" <
> >> "\xBE", not "" > "\xBE".
> >
> > Strings effectively have an implicit character at the end that's less
> > than any other character. Easy fix: Append a character that's greater
> > than any other. So "" < "A" becomes "\xFF" > "\xBE\xFF".
> >
> > Still not going to be particularly efficient.
>
> Not to mention that it still has bugs:
>
> "" < "\0"
> "\xff" < "\xff\xff"
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>
> It probably could be a list of tuples:
>
> 'abc' -> [ (1, chr(255 - ord('a')), (1, chr(255 - ord('b')), (1, chr(255
> - ord('c')), (0, '') ]
>
> ...where the (0, '') is an "end of string" marker, but it's getting
> still slower, and quite a lot bigger, and getting so reductionistic
> isn't a good thing when one class wraps another wraps another - when
> their comparison methods are interrelated.
>
I think I've found a solution:
class NegStr:
def __init__(self, value):
self._value = value
def __lt__(self, other):
return self._value > other._value
so when sorting a list of tuples:
def make_key(t):
return NegStr(t[0]), t[1]
items = [("one", 1), ("two", 2), ("three", 3), ("four", 4),
("five", 5)]
items.sort(key=make_key)
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Guido rethinking removal of cmp from sort method MRAB <python@mrabarnett.plus.com> - 2011-03-30 00:06 +0100
csiph-web