Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

creating an artificial "last element" in sort list

26 views
Skip to first unread message

dave

unread,
Sep 28, 2012, 7:39:33 PM9/28/12
to
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... 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!!!

Ian Kelly

unread,
Sep 28, 2012, 7:45:43 PM9/28/12
to Python
It appears last when I run the code.

To answer your question, though, if you want to force x to be last,
then I suggest removing it from the list and then appending it to the
end.

dave

unread,
Sep 28, 2012, 7:51:10 PM9/28/12
to Python
more clearer, this is a more realistic use case:

['awefawef', 'awefawfsf', 'awefsdf', 'zzzzzzzzzzzzzz', 'zzzzzzzzzzzzzz', 'zzzzzzzzzzzzzz']

and the quantity of ''zzzzzzzzzzzzzz'' would be dynamic.

dave

unread,
Sep 28, 2012, 7:51:10 PM9/28/12
to comp.lan...@googlegroups.com, Python
more clearer, this is a more realistic use case:

['awefawef', 'awefawfsf', 'awefsdf', 'zzzzzzzzzzzzzz', 'zzzzzzzzzzzzzz', 'zzzzzzzzzzzzzz']

and the quantity of ''zzzzzzzzzzzzzz'' would be dynamic.

88888 Dihedral

unread,
Sep 28, 2012, 8:24:59 PM9/28/12
to Python
dave於 2012年9月29日星期六UTC+8上午7時51分10秒寫道:
I am thinking if it is helpful to preprocess an arbitrary
list first into some set of unique ordered elements before a sort.

Anyway lists are passed by references to functions in python.

88888 Dihedral

unread,
Sep 28, 2012, 8:24:59 PM9/28/12
to comp.lan...@googlegroups.com, Python
dave於 2012年9月29日星期六UTC+8上午7時51分10秒寫道:

Demian Brecht

unread,
Sep 28, 2012, 8:32:17 PM9/28/12
to dave, Python
Apparently gmail hates me and my last response didn't get through:

a = ['awefawef', 'awefawfsf', 'awefsdf', 'zzzzzzzzzzzzzz',
'zzzzzzzzzzzzzz', 'zzzzzzzzzzzzzz']
f = filter(lambda s: s == a[-1], a)
l = sorted(lst[:-len(f)]) + f

Now, not 100% sure about efficiency over large sizes of a, but that's a
naive stab at it anyway.

--
Demian Brecht
@demianbrecht
http://demianbrecht.github.com

Demian Brecht

unread,
Sep 28, 2012, 8:59:48 PM9/28/12
to dave, Python
> f = filter(lambda s: s == a[-1], a)

That line's assuming that the last element may also be found in arbitrary locations 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.

Demian Brecht

unread,
Sep 28, 2012, 8:59:48 PM9/28/12
to comp.lan...@googlegroups.com, dave, Python
> f = filter(lambda s: s == a[-1], a)

duncan smith

unread,
Sep 28, 2012, 9:19:35 PM9/28/12
to
On 29/09/12 00:51, dave wrote:
> more clearer, this is a more realistic use case:
>
> ['awefawef', 'awefawfsf', 'awefsdf', 'zzzzzzzzzzzzzz', 'zzzzzzzzzzzzzz', 'zzzzzzzzzzzzzz']
>
> and the quantity of ''zzzzzzzzzzzzzz'' would be dynamic.
>

Maybe,

class Greatest:

def __lt__(self, other):
return False

def __eq__(self, other):
return type(other) == type(self)

etc.

Duncan

Paul Rubin

unread,
Sep 28, 2012, 9:42:27 PM9/28/12
to
dave <david...@gmail.com> 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

unread,
Sep 28, 2012, 10:05:15 PM9/28/12
to
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...

How about this?

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

unread,
Sep 28, 2012, 11:29:53 PM9/28/12
to Python
On Fri, Sep 28, 2012 at 6:59 PM, Demian Brecht <demian...@gmail.com> wrote:
>> f = filter(lambda s: s == a[-1], a)
>
> That line's assuming that the last element may also be found in arbitrary locations 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.
0 new messages