Velocity Reviews > creating an artificial "last element" in sort list

# creating an artificial "last element" in sort list

Paul Rubin
Guest
Posts: n/a

 09-29-2012
dave <(E-Mail Removed)> writes:
> What does x need to be to always be last on an ascending sort no
> matter what 'a' and 'b' are.... within reason...

Why are you trying to do that? It sounds ugly. Just sort the list with
the a's and b's. If you absolutely have to, you could make a class with
comparison methods that put all the x's at the bottom, but look for
something cleaner.

Steven D'Aprano
Guest
Posts: n/a

 09-29-2012
On Fri, 28 Sep 2012 16:39:33 -0700, dave wrote:

> a = ['a', 'b', x]
> b = sorted(a)
>
> What does x need to be to always be last on an ascending sort no matter
> what 'a' and 'b' are.... within reason...

a = ['a', 'b']
b = sorted(a) + ['whatever you want']

You could also do this:

x = max(a)
a.append(x)
b = sorted(a)

> I am expecting 'a' and 'b'
> will be not longer than 10 char's long.... I tried making x =
> 'zzzzzzzzzzzzzzzz' and believe it or not, this appears FIRST on the
> sort!!!

I think you are mistaken.

py> sorted(['a', 'b', 'zzzzzzzzzzzzzzzz'])
['a', 'b', 'zzzzzzzzzzzzzzzz']

But really, I don't understand what problem you are trying to solve.
Perhaps if you explain the purpose of this, we can suggest a solution.

--
Steven

Ian Kelly
Guest
Posts: n/a

 09-29-2012
On Fri, Sep 28, 2012 at 6:59 PM, Demian Brecht <(E-Mail Removed)> wrote:
>> f = filter(lambda s: s == a[-1], a)

>
> That line's assuming that the last element may also be found in arbitrarylocations in the list. If it's guaranteed that they're all contiguous at the upper bounds, I'd just walk the list backwards until I found one that wasn't matching rather than filtering.

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