# creating an artificial "last element" in sort list

Discussion in 'Python' started by dave, Sep 29, 2012.

1. ### daveGuest

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!!!

dave, Sep 29, 2012

2. ### Ian KellyGuest

On Fri, Sep 28, 2012 at 5:39 PM, 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... I am expecting 'a' and 'b' will benot longer than 10 char's long.... I tried making x = 'zzzzzzzzzzzzzzzz'and believe it or not, this appears FIRST on the sort!!!

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.

Ian Kelly, Sep 29, 2012

3. ### daveGuest

more clearer, this is a more realistic use case:

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

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

On Friday, September 28, 2012 4:46:15 PM UTC-7, Ian wrote:

>
> > a = ['a', 'b', x]

>
> >

>
> > b = sorted(a)

>
> >

>
> > What does x need to be to always be last on an ascending sort no matterwhat '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!!!

>
>
>
> 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, Sep 29, 2012
4. ### daveGuest

more clearer, this is a more realistic use case:

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

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

On Friday, September 28, 2012 4:46:15 PM UTC-7, Ian wrote:

>
> > a = ['a', 'b', x]

>
> >

>
> > b = sorted(a)

>
> >

>
> > What does x need to be to always be last on an ascending sort no matterwhat '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!!!

>
>
>
> 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, Sep 29, 2012
5. ### 88888 DihedralGuest

daveæ–¼ 2012å¹´9æœˆ29æ—¥æ˜ŸæœŸå…­UTC+8ä¸Šåˆ7æ™‚51åˆ†10ç§’å¯«é“ï¼š
> more clearer, this is a more realistic use case:
>
>
>
> ['awefawef', 'awefawfsf', 'awefsdf', 'zzzzzzzzzzzzzz', 'zzzzzzzzzzzzzz', 'zzzzzzzzzzzzzz']
>
>
>
> and the quantity of ''zzzzzzzzzzzzzz'' would be dynamic.
>
>
>
> On Friday, September 28, 2012 4:46:15 PM UTC-7, Ian 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... 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!!!

>
> >

>
> >

>
> >

>
> > 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.

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, Sep 29, 2012
6. ### 88888 DihedralGuest

daveæ–¼ 2012å¹´9æœˆ29æ—¥æ˜ŸæœŸå…­UTC+8ä¸Šåˆ7æ™‚51åˆ†10ç§’å¯«é“ï¼š
> more clearer, this is a more realistic use case:
>
>
>
> ['awefawef', 'awefawfsf', 'awefsdf', 'zzzzzzzzzzzzzz', 'zzzzzzzzzzzzzz', 'zzzzzzzzzzzzzz']
>
>
>
> and the quantity of ''zzzzzzzzzzzzzz'' would be dynamic.
>
>
>
> On Friday, September 28, 2012 4:46:15 PM UTC-7, Ian 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... 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!!!

>
> >

>
> >

>
> >

>
> > 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.

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, Sep 29, 2012
7. ### Demian BrechtGuest

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, Sep 29, 2012
8. ### Demian BrechtGuest

> 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 theupper bounds, I'd just walk the list backwards until I found one that wasn't matching rather than filtering.

Demian Brecht, Sep 29, 2012
9. ### Demian BrechtGuest

> 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 theupper bounds, I'd just walk the list backwards until I found one that wasn't matching rather than filtering.

Demian Brecht, Sep 29, 2012
10. ### duncan smithGuest

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

duncan smith, Sep 29, 2012
11. ### Paul RubinGuest

dave <> 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.

Paul Rubin, Sep 29, 2012
12. ### Steven D'ApranoGuest

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

Steven D'Aprano, Sep 29, 2012
13. ### Ian KellyGuest

On Fri, Sep 28, 2012 at 6:59 PM, Demian Brecht <> 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.

Ian Kelly, Sep 29, 2012