creating an artificial "last element" in sort list

Demian Brecht demianbrecht at gmail.com
Sat Sep 29 00:21:01 EDT 2012


On Fri, Sep 28, 2012 at 8:29 PM, Ian Kelly <ian.g.kelly at gmail.com> wrote:

> The slicing operation in the second line assumes that they're all
> collected at the end of the list anyway.
>


True enough. Hadn't considered otherwise when I first whipped that off with
the first example (thinking/trying it out *before* posting would likely be
a good idea ;)).

>>> a = ['z'*5, 'b', 'a', 'c', 'z'*5]
>>> b = filter(lambda n: n == a[-1], a)
>>> c = filter(lambda n: n != a[-1], a)
>>> d = sorted(c) + b
['a', 'b', 'c', 'zzzzz', 'zzzzz']

It does the trick for what he was asking in the original question, although
there are likely more optimal solutions for a sufficiently large a.

As Steven noted, it definitely would help to get a little more information
about the specific problem.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20120928/42bb2dbf/attachment.html>


More information about the Python-list mailing list