Raymond Hettinger wrote: >For Py2.4, I propose adding an optional list.sort() argument to >support the decorate-sort-undecorate pattern. > ... >def sort(self, cmpfunc=None, key=None): > > ... > if key is None: > self.sort(*args) > else: > aux = zip(map(key, self), self) # Decorate > aux.sort(*args) > self[:] = list(zip(*aux)[1]) # Un-decorate > If the argument is for simplicity, do we need to make this stable? Will warning about incomparables be sufficient? I'm thinking about: data = [(1-1j), -2, 1, 1j] data.sort(key=abs) Or would we prefer the code to end: else: # Decorate aux = [(key(el), nbr, el) for nbr, el in enumerate(self)] aux.sort(*args) self[:] = list(zip(*aux)[2]) # Un-decorate I think the answer comes down to performance vs. law of least surprise. I suppose I am slightly in favor of throwing in the stabilizing count (fewer explanations; those who need speed can do it themselves. -Scott David Daniels Scott.Daniels at Acm.Org
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4